Search in sources :

Example 6 with OAuth2Token

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2Token in project entando-core by entando.

the class OAuth2TokenDAO method getAccessToken.

@Override
public OAuth2Token getAccessToken(final String accessToken) throws ApsSystemException {
    Connection conn = null;
    OAuth2Token token = null;
    PreparedStatement stat = null;
    ResultSet res = null;
    try {
        conn = this.getConnection();
        stat = conn.prepareStatement(SELECT_TOKEN);
        stat.setString(1, accessToken);
        res = stat.executeQuery();
        if (res.next()) {
            token = new OAuth2Token();
            token.setAccessToken(accessToken);
            token.setRefreshToken(res.getString("refreshtoken"));
            token.setClientId(res.getString("clientid"));
            token.setGrantType(res.getString("granttype"));
            token.setExpiresIn(res.getTimestamp("expiresin"));
        }
    } catch (ApsSystemException | SQLException t) {
        logger.error("Error while loading token {}", accessToken, t);
        throw new ApsSystemException("Error while loading token " + accessToken, t);
    } finally {
        closeDaoResources(res, stat, conn);
    }
    return token;
}
Also used : OAuth2Token(org.entando.entando.aps.system.services.oauth2.model.OAuth2Token) ApsSystemException(com.agiletec.aps.system.exception.ApsSystemException)

Example 7 with OAuth2Token

use of org.entando.entando.aps.system.services.oauth2.model.OAuth2Token in project entando-core by entando.

the class ApiOAuth2TokenManager method getAccessToken.

protected OAuth2AccessToken getAccessToken(String principal, String clientId, String grantType) {
    String tokenPrefix = principal + System.nanoTime();
    final String accessToken = DigestUtils.md5Hex(tokenPrefix + "_accessToken");
    final String refreshToken = DigestUtils.md5Hex(tokenPrefix + "_refreshToken");
    final OAuth2AccessTokenImpl oAuth2Token = new OAuth2AccessTokenImpl(accessToken);
    oAuth2Token.setRefreshToken(new DefaultOAuth2RefreshToken(refreshToken));
    oAuth2Token.setClientId(clientId);
    oAuth2Token.setGrantType(grantType);
    oAuth2Token.setLocalUser(principal);
    // gets a calendar using the default time zone and locale.
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.SECOND, this.getAccessTokenValiditySeconds());
    oAuth2Token.setExpiration(calendar.getTime());
    return oAuth2Token;
}
Also used : DefaultOAuth2RefreshToken(org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken) Calendar(java.util.Calendar) OAuth2AccessTokenImpl(org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl)

Aggregations

OAuth2Token (org.entando.entando.aps.system.services.oauth2.model.OAuth2Token)5 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)4 Calendar (java.util.Calendar)4 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3 MD5Generator (org.apache.oltu.oauth2.as.issuer.MD5Generator)2 OAuthIssuer (org.apache.oltu.oauth2.as.issuer.OAuthIssuer)2 OAuthIssuerImpl (org.apache.oltu.oauth2.as.issuer.OAuthIssuerImpl)2 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 OAuthAccessResourceRequest (org.apache.oltu.oauth2.rs.request.OAuthAccessResourceRequest)2 IApiOAuth2TokenManager (org.entando.entando.aps.system.services.oauth2.IApiOAuth2TokenManager)2 OAuth2AccessTokenImpl (org.entando.entando.aps.system.services.oauth2.model.OAuth2AccessTokenImpl)2 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)2 IAuthorizationManager (com.agiletec.aps.system.services.authorization.IAuthorizationManager)1 Role (com.agiletec.aps.system.services.role.Role)1 IUserManager (com.agiletec.aps.system.services.user.IUserManager)1 UserDetails (com.agiletec.aps.system.services.user.UserDetails)1 EntandoTokenException (org.entando.entando.web.common.exceptions.EntandoTokenException)1