Example usage for org.springframework.security.oauth2.common OAuth2AccessToken getClass

List of usage examples for org.springframework.security.oauth2.common OAuth2AccessToken getClass

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common OAuth2AccessToken getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.cloudfoundry.identity.uaa.audit.event.TokenIssuedEvent.java

public TokenIssuedEvent(OAuth2AccessToken source, Authentication principal) {
    super(source, principal);
    if (!OAuth2AccessToken.class.isAssignableFrom(source.getClass())) {
        throw new IllegalArgumentException();
    }/*from ww  w  . j av a 2 s  .c o  m*/
}

From source file:com.avego.oauth.migration.OauthDataMigrator.java

/**
 * This migrates the oauth access tokens
 * @throws IOException If an IO error occurs such as when serializing/deserializing data
 * @throws ClassNotFoundException If a class not found when serializing/deserializing data
 * @throws InvocationTargetException If an invocation target exception occurs when using reflection to convert to the new objects
 * @throws IllegalAccessException If an IllegalAccessException exception occurs when using reflection to convert to the new objects
 * @throws NoSuchMethodException If a NoSuchMethodException exception occurs when using reflection to convert to the new objects
 * @throws InstantiationException// ww w .  j  a  v a 2  s .c  o  m
 * @throws IllegalArgumentException
 */
@SuppressWarnings("unchecked")
protected void migrateAccessTokens() throws IOException, ClassNotFoundException, NoSuchMethodException,
        IllegalAccessException, InvocationTargetException, IllegalArgumentException, InstantiationException {

    int numTokens = this.dao.countUnmigratedAccessTokens();
    int pageSize = PAGE_SIZE;

    int numMigrated = 0;
    System.out.println("Starting Migrating " + numTokens + " access token(s) ...");

    while (numTokens > 0) {

        List<OauthAccessTokenRecord> accessTokens = this.dao.getUnmigratedOauthAccessTokenRecords(pageSize);

        for (OauthAccessTokenRecord tokenRecord : accessTokens) {

            String oldTokenId = tokenRecord.getTokenId();
            System.out.println("Migrating token with id: " + oldTokenId + "...");

            String newTokenId = this.dao.generateNewTokenKey(tokenRecord.getTokenId());
            String newRefreshToken = this.dao.generateNewTokenKey(tokenRecord.getRefreshToken());

            if (this.removeRefreshTokens) {
                newRefreshToken = null;
            }

            System.out.println("New token id: " + newTokenId);
            System.out.println("New refresh token id: " + newRefreshToken);

            // deserialize the token, note this is backward compatible
            OAuth2AccessToken accessToken = null;
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(tokenRecord.getToken()));
            try {
                Object obj = ois.readObject();
                accessToken = (OAuth2AccessToken) obj;
            } finally {
                ois.close();
            }

            // replace the token value in the access token..
            if (this.serializeNewTokenValues) {

                Constructor<OAuth2AccessToken> constructor = null;

                // see if it has a set value method
                Method setValueMethod = MethodUtils.getAccessibleMethod(accessToken.getClass(), "setValue",
                        String.class);
                if (setValueMethod != null) {
                    Object res = setValueMethod.invoke(accessToken, newTokenId);
                    if (res != null && res instanceof OAuth2AccessToken) {
                        accessToken = (OAuth2AccessToken) res;
                    }
                } else {

                    // look for constructors that we can use
                    constructor = (Constructor<OAuth2AccessToken>) ConstructorUtils
                            .getAccessibleConstructor(accessToken.getClass(), String.class);
                    if (constructor != null) {

                        OAuth2AccessToken newAccessToken = constructor.newInstance(newTokenId);

                        // we also need to invoke setters for other fields
                        MethodUtils.invokeMethod(newAccessToken, "setAdditionalInformation",
                                accessToken.getAdditionalInformation());
                        MethodUtils.invokeMethod(newAccessToken, "setExpiration", accessToken.getExpiration());
                        MethodUtils.invokeMethod(newAccessToken, "setScope", accessToken.getScope());
                        MethodUtils.invokeMethod(newAccessToken, "setTokenType", accessToken.getTokenType());

                        accessToken = newAccessToken;

                    } else {
                        throw new IllegalStateException("The access token with the class: "
                                + accessToken.getClass().getName()
                                + " did not have a set value method nor a constructor taking a string which we need to update its token value");
                    }

                }

                // we also need to overwrite the refresh token
                String newRefreshTokenValue = this.dao
                        .generateNewTokenKey(accessToken.getRefreshToken().getValue());
                OAuth2RefreshToken refreshToken = replaceOAuth2RefreshTokenValue(accessToken.getRefreshToken(),
                        newRefreshTokenValue);
                MethodUtils.invokeMethod(accessToken, "setRefreshToken", refreshToken);
            }

            if (this.removeRefreshTokens) {
                MethodUtils.invokeMethod(accessToken, "setRefreshToken", new Object[] { null },
                        new Class<?>[] { OAuth2RefreshToken.class });
            }

            byte[] tokenData = SerializationUtils.serialize((Serializable) accessToken);

            // deserialise the authenticated, this is NOT backward compatible so we have to read using a diff class loader
            OAuth2Authentication auth = deserializeOAuth2Authentication(tokenRecord.getAuthentication());
            byte[] authData = SerializationUtils.serialize(auth);

            // this does the actual migration of the token
            this.dao.updateOauthAccessToken(oldTokenId, newTokenId, newRefreshToken, tokenData, authData);

            System.out.println("Migrated token with id: " + oldTokenId);
            numMigrated++;
            System.out.println("");
        }
        numTokens = this.dao.countUnmigratedAccessTokens();
    }

    System.out.println("Finished Migrating " + numMigrated + " access token(s).");

}