@Override protected void onBeforeRequest( HttpRequest request ) throws IOException { if (oauth2 != null) { request.setInterceptor(request1 -> { new BasicAuthentication( USERNAME, PASSWORD ).intercept(request1); oauth2.intercept(request1); }); } else { request.setInterceptor(request1 -> { new BasicAuthentication( USERNAME, PASSWORD ).intercept(request1); }); } }
private Credential createCredentialWithRefreshToken( HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) { String clientId = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getClientId(); String clientSecret = settingsService.getSettings().getCalendarSettings().getGoogleCalendarSettings().getClientSecret(); return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport( transport) .setJsonFactory(jsonFactory) .setTokenServerUrl( new GenericUrl(GOOGLEAPIS_OAUTH2_V4_TOKEN)) .setClientAuthentication(new BasicAuthentication( clientId, clientSecret)) .build() .setFromTokenResponse(tokenResponse); }
RefreshTokenRequest newRequest( OauthClient oauthClient, String refreshTokenSecret, String tokenEndpoint) { RefreshTokenRequest result = new RefreshTokenRequest( new NetHttpTransport(), new JacksonFactory(), new GenericUrl(tokenEndpoint), refreshTokenSecret); result.setClientAuthentication( new BasicAuthentication(oauthClient.getId(), oauthClient.getSecret())); return result; }
public AbstractService(final AlfrescoConfig pConfig) { mBaseUrl = new GenericUrl(pConfig.getHost()); mHttpRequestFactory = HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) { request .setParser(new JsonObjectParser(JSON_FACTORY)) .setInterceptor(new BasicAuthentication(pConfig.getUsername(), pConfig.getPassword())); } }); }
public static HttpRequestFactory getRequestFactory(final AlfrescoConfig pConfig) { return HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) { request .setParser(new JsonObjectParser(JSON_FACTORY)) .setInterceptor(new BasicAuthentication(pConfig.getUsername(), pConfig.getPassword())); } }); }
private UsersBean checkLogin(final String pStrUsername, final String pStrPassword){ Person lPerson = null; try { AlfrescoConfig lAlfrescoConfig = Util.getDefaultAlfrescoConfig(pStrUsername,pStrPassword); HttpRequestFactory lHttpRequestFactory = AlfrescoHelper.HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) { request .setParser(new JsonObjectParser(AlfrescoHelper.JSON_FACTORY)) .setInterceptor(new BasicAuthentication(pStrUsername, pStrPassword)); } }); lPerson = AlfrescoHelper.getUser(pStrUsername, lHttpRequestFactory, lAlfrescoConfig); } catch (Exception e) { return null; // TODO: gestire meglio } if (lPerson == null || !lPerson.isEnabled()) { return null; } UsersBean lUsersBean = new UsersBean(); lUsersBean.setUsername(pStrUsername); lUsersBean.setName(lPerson.getFirstName()); lUsersBean.setSurname(lPerson.getLastName()); lUsersBean.setFullName(StringUtils.defaultString(lPerson.getFirstName())+" "+StringUtils.defaultString(lPerson.getLastName())); lUsersBean.setEnabled(1); lUsersBean.setIdUser((int) Calendar.getInstance().getTimeInMillis()); //FIXME? return lUsersBean; }
/** * Exchanges a Refresh Token for a new set of tokens. * * Note that the Token Server may require you to use the `offline_access` scope to receive * Refresh Tokens. * * @param refreshToken the refresh token used to request new Access Token / idToken. * @return the parsed successful token response received from the token endpoint * @throws IOException for an error response */ public TokenResponse refreshTokens(String refreshToken) throws IOException { List<String> scopesList = Arrays.asList(scopes); RefreshTokenRequest request = new RefreshTokenRequest( AndroidHttp.newCompatibleTransport(), new GsonFactory(), new GenericUrl(tokenEndpoint), refreshToken); if (!scopesList.isEmpty()) { request.setScopes(scopesList); } // This are extra query parameters that can be specific to an OP. For instance prompt -> consent // tells the Authorization Server that it SHOULD prompt the End-User for consent before returning // information to the Client. if (extras != null) { for (Map.Entry<String, String> queryParam : extras.entrySet()) { request.set(queryParam.getKey(), queryParam.getValue()); } } // If the oidc client is confidential (needs authentication) if (!TextUtils.isEmpty(clientSecret)) { request.setClientAuthentication(new BasicAuthentication(clientId, clientSecret)); } else { request.set("client_id", clientId); } if (useOAuth2) { if (scopesList.contains("openid")) { Log.w(TAG, "Using OAuth2 only request but scopes contain values for OpenId Connect"); } return request.executeUnparsed().parseAs(TokenResponse.class); } else { return IdTokenResponse.execute(request); } }
public FitbitUserCredentialManager( DataStoreFactory dataStoreFactory, FitbitClientCredentials credentials) throws IOException { this.dataStoreFactory = dataStoreFactory; this.credentials = credentials; this.basicAuthentication = new BasicAuthentication(credentials.getId(), credentials.getSecret()); this.flow = new AuthorizationCodeFlow.Builder( BearerToken.authorizationHeaderAccessMethod(), transport, gsonFactory, tokenEndPoint, this.basicAuthentication, this.credentials.getId(), FitbitApiConfig.AUTHORIZE_URL) .setScopes(Arrays.asList("activity")) .setDataStoreFactory(dataStoreFactory) .build(); this.credentialBuilder = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()) .setClientAuthentication(this.basicAuthentication) .setJsonFactory(gsonFactory) .setTransport(transport) .setTokenServerUrl(tokenEndPoint); }
/** * Asynchronously attempt to acquire an OAuth Access Token * * @param cb called when AccessToken is acquired. Always called * on a background thread suitable for networking. */ protected void acquireAccessToken(final OAuthCallback cb) { if (isAccessTokenCached()) { // Execute the callback immediately with cached OAuth credentials if (VERBOSE) Log.d(TAG, "Access token cached"); if (cb != null) { // Ensure networking occurs off the main thread // TODO: Use an ExecutorService and expose an application shutdown method to shutdown? new Thread(new Runnable() { @Override public void run() { cb.onSuccess(getRequestFactoryFromCachedCredentials()); } }).start(); } } else if (mOauthInProgress && cb != null) { // Add the callback to the queue for execution when outstanding OAuth negotiation complete mCallbackQueue.add(cb); if (VERBOSE) Log.i(TAG, "Adding cb to queue"); } else { mOauthInProgress = true; // Perform an OAuth Client Credentials Request // TODO: Replace with new Thread() new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { TokenResponse response = null; try { if (VERBOSE) Log.i(TAG, "Fetching OAuth " + mConfig.getAccessTokenRequestUrl()); response = new ClientCredentialsTokenRequest(new NetHttpTransport(), new JacksonFactory(), new GenericUrl(mConfig.getAccessTokenRequestUrl())) .setGrantType("client_credentials") .setClientAuthentication(new BasicAuthentication(mConfig.getClientId(), mConfig.getClientSecret())) .execute(); } catch (IOException e) { // TODO: Alert user Kickflip down // or client credentials invalid if (cb != null) { postExceptionToCallback(cb, e); } e.printStackTrace(); } if (response != null) { if (VERBOSE) Log.i(TAG, "Got Access Token " + response.getAccessToken().substring(0, 5) + "..."); storeAccessToken(response); mOauthInProgress = false; if (cb != null) cb.onSuccess(getRequestFactoryFromAccessToken(mStorage.getString(ACCESS_TOKEN_KEY, null))); executeQueuedCallbacks(); } else { mOauthInProgress = false; Log.w(TAG, "Failed to get Access Token"); } return null; } }.execute(); } }
/** * Returns a Bitbucket REST API service with a user name and a password for * Basic authentication. * @param user user name * @param password password * @return Bitbucket REST API service * @throws NullPointerException if either the user name or the password is * <code>null</code> * @since 4.0 */ public Service getService(String user, String password) { return new RestService(new BasicAuthentication(user, password)); }
/** * Returns a HTTP execute interceptor with the client identifier and the * client secret for Basic authentication. * @return HTTP execute interceptor */ protected HttpExecuteInterceptor getBasicAuthentication() { return new BasicAuthentication(getClientId(), getClientSecret()); }