Search in sources :

Example 1 with IntrospectInfo

use of org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method getTokenMetaData.

@Override
public AccessTokenInfo getTokenMetaData(String accessToken) throws APIManagementException {
    AccessTokenInfo tokenInfo = new AccessTokenInfo();
    try {
        IntrospectInfo introspectInfo = introspectionClient.introspect(accessToken);
        tokenInfo.setAccessToken(accessToken);
        boolean isActive = introspectInfo.isActive();
        if (!isActive) {
            tokenInfo.setTokenValid(false);
            tokenInfo.setErrorcode(APIConstants.KeyValidationStatus.API_AUTH_INVALID_CREDENTIALS);
            return tokenInfo;
        }
        tokenInfo.setTokenValid(true);
        if (introspectInfo.getIat() > 0 && introspectInfo.getExpiry() > 0) {
            if (introspectInfo.getExpiry() != Long.MAX_VALUE) {
                long validityPeriod = introspectInfo.getExpiry() - introspectInfo.getIat();
                tokenInfo.setValidityPeriod(validityPeriod * 1000L);
            } else {
                tokenInfo.setValidityPeriod(Long.MAX_VALUE);
            }
            tokenInfo.setIssuedTime(introspectInfo.getIat() * 1000L);
        }
        if (StringUtils.isNotEmpty(introspectInfo.getScope())) {
            String[] scopes = introspectInfo.getScope().split(" ");
            tokenInfo.setScope(scopes);
        }
        tokenInfo.setConsumerKey(introspectInfo.getClientId());
        String username = introspectInfo.getUsername();
        if (!StringUtils.isEmpty(username)) {
            tokenInfo.setEndUserName(username);
        }
        return tokenInfo;
    } catch (KeyManagerClientException e) {
        throw new APIManagementException("Error occurred in token introspection!", e);
    }
}
Also used : AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) KeyManagerClientException(org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) IntrospectInfo(org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo)

Example 2 with IntrospectInfo

use of org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImplTest method testTokenUnlimitedExpirationTime.

@Test
public void testTokenUnlimitedExpirationTime() throws KeyManagerClientException, APIManagementException {
    String accessToken = "155ddde3-68db-35b1-82dc-1247616b2da9";
    IntrospectInfo response = new IntrospectInfo();
    response.setActive(true);
    response.setExpiry(Long.MAX_VALUE);
    response.setIat(new Date().getTime());
    Mockito.when(introspectionClient.introspect(accessToken)).thenReturn(response);
    AccessTokenInfo info = keyManager.getTokenMetaData(accessToken);
    Assert.assertEquals(Long.MAX_VALUE, info.getValidityPeriod());
}
Also used : AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) IntrospectInfo(org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo) Date(java.util.Date) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

AccessTokenInfo (org.wso2.carbon.apimgt.api.model.AccessTokenInfo)2 IntrospectInfo (org.wso2.carbon.apimgt.impl.kmclient.model.IntrospectInfo)2 Date (java.util.Date)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 KeyManagerClientException (org.wso2.carbon.apimgt.impl.kmclient.KeyManagerClientException)1