Java源码示例:net.openid.appauth.TokenRequest
示例1
@MainThread
private void performTokenRequest(
TokenRequest request,
AuthorizationService.TokenResponseCallback callback) {
ClientAuthentication clientAuthentication;
try {
clientAuthentication = mStateManager.getCurrent().getClientAuthentication();
} catch (ClientAuthentication.UnsupportedAuthenticationMethod ex) {
Log.d(TAG, "Token request cannot be made, client authentication for the token "
+ "endpoint could not be constructed (%s)", ex);
displayNotAuthorized("Client authentication method is unsupported");
return;
}
mAuthService.performTokenRequest(
request,
clientAuthentication,
callback);
}
示例2
@Override
public void run() {
if(MyApp.Token == null)
return;
final AuthManager authManager = AuthManager.getInstance(TokenService.this);
final AuthState authState = authManager.getAuthState();
if(authState.getNeedsTokenRefresh()) {
//Get New Token
ClientSecretPost clientSecretPost = new ClientSecretPost(authManager.getAuth().getClientSecret());
final TokenRequest request = authState.createTokenRefreshRequest();
final AuthorizationService authService = authManager.getAuthService();
authService.performTokenRequest(request, clientSecretPost, new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable AuthorizationException ex) {
if(ex != null){
ex.printStackTrace();
return;
}
authManager.updateAuthState(response,ex);
MyApp.Token = authState.getIdToken();
}
});
}
}
示例3
@PluginMethod()
public void refreshToken(final PluginCall call) {
disposeAuthService();
OAuth2RefreshTokenOptions oAuth2RefreshTokenOptions = buildRefreshTokenOptions(call.getData());
if (oAuth2RefreshTokenOptions.getAppId() == null) {
call.reject(ERR_PARAM_NO_APP_ID);
return;
}
if (oAuth2RefreshTokenOptions.getAccessTokenEndpoint() == null) {
call.reject(ERR_PARAM_NO_ACCESS_TOKEN_ENDPOINT);
return;
}
if (oAuth2RefreshTokenOptions.getRefreshToken() == null) {
call.reject(ERR_PARAM_NO_REFRESH_TOKEN);
return;
}
this.authService = new AuthorizationService(getContext());
AuthorizationServiceConfiguration config = new AuthorizationServiceConfiguration(
Uri.parse(""),
Uri.parse(oAuth2RefreshTokenOptions.getAccessTokenEndpoint())
);
if (this.authState == null) {
this.authState = new AuthState(config);
}
TokenRequest tokenRequest = new TokenRequest.Builder(
config,
oAuth2RefreshTokenOptions.getAppId()
).setGrantType(GrantTypeValues.REFRESH_TOKEN)
.setScope(oAuth2RefreshTokenOptions.getScope())
.setRefreshToken(oAuth2RefreshTokenOptions.getRefreshToken())
.build();
this.authService.performTokenRequest(tokenRequest, (response1, ex) -> {
this.authState.update(response1, ex);
if (ex != null) {
call.reject(ERR_GENERAL, ex);
} else {
if (response1 != null) {
try {
JSObject json = new JSObject(response1.jsonSerializeString());
call.resolve(json);
} catch (JSONException e) {
call.reject(ERR_GENERAL, e);
}
} else {
call.reject(ERR_NO_ACCESS_TOKEN);
}
}
});
}
示例4
@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (data == null) {
if (promise != null) {
promise.reject("authentication_error", "Data intent is null" );
}
return;
}
final AuthorizationResponse response = AuthorizationResponse.fromIntent(data);
AuthorizationException exception = AuthorizationException.fromIntent(data);
if (exception != null) {
if (promise != null) {
promise.reject("authentication_error", getErrorMessage(exception));
}
return;
}
final Promise authorizePromise = this.promise;
final AppAuthConfiguration configuration = createAppAuthConfiguration(
createConnectionBuilder(this.dangerouslyAllowInsecureHttpRequests, this.tokenRequestHeaders)
);
AuthorizationService authService = new AuthorizationService(this.reactContext, configuration);
TokenRequest tokenRequest = response.createTokenExchangeRequest(this.additionalParametersMap);
AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(
TokenResponse resp, AuthorizationException ex) {
if (resp != null) {
WritableMap map = TokenResponseFactory.tokenResponseToMap(resp, response);
if (authorizePromise != null) {
authorizePromise.resolve(map);
}
} else {
if (promise != null) {
promise.reject("token_exchange_failed", getErrorMessage(ex));
}
}
}
};
if (this.clientSecret != null) {
ClientAuthentication clientAuth = this.getClientAuthentication(this.clientSecret, this.clientAuthMethod);
authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
} else {
authService.performTokenRequest(tokenRequest, tokenResponseCallback);
}
}
}
示例5
private void refreshWithConfiguration(
final AuthorizationServiceConfiguration serviceConfiguration,
final AppAuthConfiguration appAuthConfiguration,
final String refreshToken,
final String clientId,
final ReadableArray scopes,
final String redirectUrl,
final Map<String, String> additionalParametersMap,
final String clientAuthMethod,
final String clientSecret,
final Promise promise
) {
String scopesString = null;
if (scopes != null) {
scopesString = this.arrayToString(scopes);
}
final Context context = this.reactContext;
TokenRequest.Builder tokenRequestBuilder =
new TokenRequest.Builder(
serviceConfiguration,
clientId
)
.setRefreshToken(refreshToken)
.setRedirectUri(Uri.parse(redirectUrl));
if (scopesString != null) {
tokenRequestBuilder.setScope(scopesString);
}
if (!additionalParametersMap.isEmpty()) {
tokenRequestBuilder.setAdditionalParameters(additionalParametersMap);
}
TokenRequest tokenRequest = tokenRequestBuilder.build();
AuthorizationService authService = new AuthorizationService(context, appAuthConfiguration);
AuthorizationService.TokenResponseCallback tokenResponseCallback = new AuthorizationService.TokenResponseCallback() {
@Override
public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable AuthorizationException ex) {
if (response != null) {
WritableMap map = TokenResponseFactory.tokenResponseToMap(response);
promise.resolve(map);
} else {
promise.reject("token_refresh_failed", getErrorMessage(ex));
}
}
};
if (clientSecret != null) {
ClientAuthentication clientAuth = this.getClientAuthentication(clientSecret, clientAuthMethod);
authService.performTokenRequest(tokenRequest, clientAuth, tokenResponseCallback);
} else {
authService.performTokenRequest(tokenRequest, tokenResponseCallback);
}
}