Java Code Examples for org.springframework.security.oauth2.common.OAuth2AccessToken#getExpiration()

The following examples show how to use org.springframework.security.oauth2.common.OAuth2AccessToken#getExpiration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomJwtTokenEnhancer.java    From fast-family-master with Apache License 2.0 6 votes vote down vote up
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
    if (oAuth2AccessToken instanceof DefaultOAuth2AccessToken) {
        DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) oAuth2AccessToken;
        String clientId = oAuth2Authentication.getOAuth2Request().getClientId();
        Date expiration = oAuth2AccessToken.getExpiration();
        String createToken = createToken(clientId, expiration);
        token.setValue(createToken);
        OAuth2RefreshToken refreshToken = oAuth2AccessToken.getRefreshToken();
        if (refreshToken instanceof DefaultOAuth2AccessToken) {
            token.setRefreshToken(new DefaultOAuth2RefreshToken(createToken(clientId, expiration)));
        }
        Map<String, Object> additionalInformation = new HashMap<>();
        additionalInformation.put("client_id", oAuth2Authentication.getOAuth2Request().getClientId());
        token.setAdditionalInformation(additionalInformation);
        return token;
    }
    return oAuth2AccessToken;
}
 
Example 2
Source File: PigRedisTokenStore.java    From pig with MIT License 5 votes vote down vote up
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {

    this.redisTemplate.opsForValue().set(ACCESS + token.getValue(), token);
    this.redisTemplate.opsForValue().set(AUTH + token.getValue(), authentication);
    this.redisTemplate.opsForValue().set(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), token);
    if (!authentication.isClientOnly()) {
        redisTemplate.opsForList().rightPush(UNAME_TO_ACCESS + getApprovalKey(authentication), token);
    }

    redisTemplate.opsForList().rightPush(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), token);

    if (token.getExpiration() != null) {

        int seconds = token.getExpiresIn();
        redisTemplate.expire(ACCESS + token.getValue(), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(AUTH + token.getValue(), seconds, TimeUnit.SECONDS);

        redisTemplate.expire(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), seconds, TimeUnit.SECONDS);
        redisTemplate.expire(UNAME_TO_ACCESS + getApprovalKey(authentication), seconds, TimeUnit.SECONDS);
    }
    if (token.getRefreshToken() != null && token.getRefreshToken().getValue() != null) {
        this.redisTemplate.opsForValue().set(REFRESH_TO_ACCESS + token.getRefreshToken().getValue(), token.getValue());
        this.redisTemplate.opsForValue().set(ACCESS_TO_REFRESH + token.getValue(), token.getRefreshToken().getValue());
    }
}
 
Example 3
Source File: FwRedisTokenStore.java    From fw-cloud-framework with MIT License 5 votes vote down vote up
@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {

	this.redisTemplate.opsForValue().set(ACCESS + token.getValue(), token);
	this.redisTemplate.opsForValue().set(AUTH + token.getValue(), authentication);
	this.redisTemplate.opsForValue().set(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), token);
	if (!authentication.isClientOnly()) {
		this.redisTemplate.opsForList().rightPush(UNAME_TO_ACCESS + getApprovalKey(authentication), token);
	}

	redisTemplate.opsForList().rightPush(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), token);

	if (token.getExpiration() != null) {

		int seconds = token.getExpiresIn();
		redisTemplate.expire(ACCESS + token.getValue(), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(AUTH + token.getValue(), seconds, TimeUnit.SECONDS);

		redisTemplate.expire(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId(), seconds, TimeUnit.SECONDS);
		redisTemplate.expire(UNAME_TO_ACCESS + getApprovalKey(authentication), seconds, TimeUnit.SECONDS);
	}
	if (token.getRefreshToken() != null && token.getRefreshToken()
			.getValue() != null) {
		this.redisTemplate.opsForValue().set(REFRESH_TO_ACCESS + token.getRefreshToken().getValue(), token.getValue());
		this.redisTemplate.opsForValue().set(ACCESS_TO_REFRESH + token.getValue(), token.getRefreshToken().getValue());
	}
}
 
Example 4
Source File: CustomAccessTokenConverter.java    From microservices-oauth with Apache License 2.0 5 votes vote down vote up
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
	Map<String, Object> response = new HashMap<String, Object>();
	OAuth2Request clientToken = authentication.getOAuth2Request();

	if (!authentication.isClientOnly())
		response.putAll(userTokenConverter.convertUserAuthentication(authentication.getUserAuthentication()));
	else if (clientToken.getAuthorities() != null && !clientToken.getAuthorities().isEmpty())
		response.put(UserAuthenticationConverter.AUTHORITIES,
				AuthorityUtils.authorityListToSet(clientToken.getAuthorities()));

	if (token.getScope() != null)
		response.put(SCOPE, token.getScope());

	if (token.getAdditionalInformation().containsKey(JTI))
		response.put(JTI, token.getAdditionalInformation().get(JTI));

	if (token.getExpiration() != null)
		response.put(EXP, token.getExpiration().getTime() / 1000);

	if (includeGrantType && authentication.getOAuth2Request().getGrantType() != null)
		response.put(GRANT_TYPE, authentication.getOAuth2Request().getGrantType());

	response.putAll(token.getAdditionalInformation());

	response.put(CLIENT_ID, clientToken.getClientId());
	if (clientToken.getResourceIds() != null && !clientToken.getResourceIds().isEmpty())
		response.put(AUD, clientToken.getResourceIds());

	return response;
}
 
Example 5
Source File: CustomAccessTokenConverter.java    From spring-boot-2-oauth2-resource-jwt with MIT License 5 votes vote down vote up
public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
	Map<String, Object> response = new HashMap<String, Object>();
	OAuth2Request clientToken = authentication.getOAuth2Request();

	if (!authentication.isClientOnly())
		response.putAll(userTokenConverter.convertUserAuthentication(authentication.getUserAuthentication()));
	else if (clientToken.getAuthorities() != null && !clientToken.getAuthorities().isEmpty())
		response.put(UserAuthenticationConverter.AUTHORITIES,
				AuthorityUtils.authorityListToSet(clientToken.getAuthorities()));

	if (token.getScope() != null)
		response.put(SCOPE, token.getScope());

	if (token.getAdditionalInformation().containsKey(JTI))
		response.put(JTI, token.getAdditionalInformation().get(JTI));

	if (token.getExpiration() != null)
		response.put(EXP, token.getExpiration().getTime() / 1000);

	if (includeGrantType && authentication.getOAuth2Request().getGrantType() != null)
		response.put(GRANT_TYPE, authentication.getOAuth2Request().getGrantType());

	response.putAll(token.getAdditionalInformation());

	response.put(CLIENT_ID, clientToken.getClientId());
	if (clientToken.getResourceIds() != null && !clientToken.getResourceIds().isEmpty())
		response.put(AUD, clientToken.getResourceIds());

	return response;
}
 
Example 6
Source File: GsonSerializerOAuth2AccessToken.java    From NFVO with Apache License 2.0 5 votes vote down vote up
@Override
public JsonElement serialize(
    OAuth2AccessToken src, Type typeOfSrc, JsonSerializationContext context) {
  JsonObject jsonObject = new JsonObject();
  jsonObject.addProperty(OAuth2AccessToken.ACCESS_TOKEN, src.getValue());
  // back compatibility for dashboard
  jsonObject.addProperty("value", src.getValue());

  jsonObject.addProperty(OAuth2AccessToken.TOKEN_TYPE, src.getTokenType());

  OAuth2RefreshToken refreshToken = src.getRefreshToken();
  if (refreshToken != null) {
    jsonObject.addProperty(OAuth2AccessToken.REFRESH_TOKEN, refreshToken.getValue());
  }
  Date expiration = src.getExpiration();
  if (expiration != null) {
    long now = System.currentTimeMillis();
    jsonObject.add(
        OAuth2AccessToken.EXPIRES_IN, new JsonPrimitive((expiration.getTime() - now) / 1000));
  }

  Set<String> scope = src.getScope();

  if (scope != null && !scope.isEmpty()) {
    StringBuilder scopes = new StringBuilder();
    for (String s : scope) {
      Assert.hasLength(s, "Scopes cannot be null or empty. Got " + scope + "");
      scopes.append(s);
      scopes.append(" ");
    }

    jsonObject.addProperty(OAuth2AccessToken.SCOPE, scopes.substring(0, scopes.length() - 1));
  }

  return jsonObject;
}