com.azure.communication.common.implementation.TokenParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of azure-communication-common Show documentation
Show all versions of azure-communication-common Show documentation
This package contains data structures commonly used for communicating with Microsoft Azure Communication Services.
For this release, see notes - https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/communication/azure-communication-common/README.md and https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/communication/azure-communication-common/CHANGELOG.md.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.communication.common.implementation;
import java.nio.charset.Charset;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Base64;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import com.azure.core.credential.AccessToken;
/**
* JWT token parser to convert raw token to Token structure
*/
public class TokenParser {
private static final String EXP_CLAIM_REGEX = "\"exp\":*([0-9]+)";
/**
* Parse token raw string provided by Communication Service authentication
* @param rawToken serialized Communication Service authentication token
* @return deserialized AccessToken
*/
public AccessToken parseJWTToken(String rawToken) {
Objects.requireNonNull(rawToken, "'rawToken' cannot be null.");
String jwt = new String(Base64.getDecoder().decode(rawToken.split("\\.")[1]), Charset.forName("UTF-8"));
Pattern pattern = Pattern.compile(EXP_CLAIM_REGEX);
Matcher matcher = pattern.matcher(jwt);
matcher.find();
long expire = Long.parseLong(matcher.group(1));
LocalDateTime expiryLocal = LocalDateTime.ofInstant(Instant.ofEpochMilli(expire * 1000), ZoneId.of("UTC"));
OffsetDateTime offsetExpiry = expiryLocal.atOffset(OffsetDateTime.now().getOffset());
return new AccessToken(rawToken, offsetExpiry);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy