Search in sources :

Example 61 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class OdnoklassnikiServiceTest method testSigGeneration.

@Test
public void testSigGeneration() {
    final OAuth2AccessToken accessToken = new OAuth2AccessToken("d3iwa.403gvrs194740652m1k4w2a503k3c");
    final OAuthRequest request = new OAuthRequest(Verb.GET, URL);
    service.signRequest(accessToken, request);
    assertEquals("96127f5ca29a8351399e94bbd284ab16", findParam(request.getQueryStringParams(), "sig"));
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) Test(org.junit.Test)

Example 62 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project pac4j by pac4j.

the class OAuthProfileCreator method sendRequestForData.

/**
 * Make a request to get the data of the authenticated user for the provider.
 *
 * @param service the OAuth service
 * @param accessToken the access token
 * @param dataUrl     url of the data
 * @param verb        method used to request data
 * @return the user data response
 */
protected String sendRequestForData(final S service, final T accessToken, final String dataUrl, Verb verb) {
    logger.debug("accessToken: {} / dataUrl: {}", accessToken, dataUrl);
    final long t0 = System.currentTimeMillis();
    final OAuthRequest request = createOAuthRequest(dataUrl, verb);
    signRequest(service, accessToken, request);
    final String body;
    final int code;
    try {
        Response response = service.execute(request);
        code = response.getCode();
        body = response.getBody();
    } catch (final IOException | InterruptedException | ExecutionException e) {
        throw new HttpCommunicationException("Error getting body: " + e.getMessage());
    }
    final long t1 = System.currentTimeMillis();
    logger.debug("Request took: " + (t1 - t0) + " ms for: " + dataUrl);
    logger.debug("response code: {} / response body: {}", code, body);
    if (code != 200) {
        throw new HttpCommunicationException(code, body);
    }
    return body;
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) HttpCommunicationException(org.pac4j.core.exception.HttpCommunicationException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 63 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project dataverse by IQSS.

the class OrcidOAuth2AP method getUserRecord.

@Override
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception {
    OAuth20Service service = getService(state, redirectUrl);
    OAuth2AccessToken accessToken = service.getAccessToken(code);
    if (!accessToken.getScope().contains(scope)) {
        // We did not get the permissions on the scope we need. Abort and inform the user.
        throw new OAuth2Exception(200, BundleUtil.getStringFromBundle("auth.providers.orcid.insufficientScope"), "");
    }
    String orcidNumber = extractOrcidNumber(accessToken.getRawResponse());
    final String userEndpoint = getUserEndpoint(accessToken);
    final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint, service);
    request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
    request.setCharset("UTF-8");
    final Response response = request.send();
    int responseCode = response.getCode();
    final String body = response.getBody();
    logger.log(Level.FINE, "In getUserRecord. Body: {0}", body);
    if (responseCode == 200) {
        final ParsedUserResponse parsed = parseUserResponse(body);
        AuthenticatedUserDisplayInfo orgData = getOrganizationalData(userEndpoint, accessToken.getAccessToken(), service);
        parsed.displayInfo.setAffiliation(orgData.getAffiliation());
        parsed.displayInfo.setPosition(orgData.getPosition());
        return new OAuth2UserRecord(getId(), orcidNumber, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails);
    } else {
        throw new OAuth2Exception(responseCode, body, "Error getting the user info record.");
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) OAuth2UserRecord(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2UserRecord) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth2Exception(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2Exception) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 64 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project adeptj-modules by AdeptJ.

the class OAuth2CallbackServlet method processRequest.

private void processRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String code = req.getParameter("code");
    LOGGER.info("OAuth2 code: [{}]", code);
    String provider = StringUtils.substringAfterLast(req.getRequestURI(), "/");
    LOGGER.info("Provider: [{}]", provider);
    OAuth20Service oAuth2Service = this.providerFactory.getOAuth2Service(provider);
    OAuth2AccessToken token = null;
    try {
        token = oAuth2Service.getAccessToken(code);
        LOGGER.info("OAuth2AccessToken: [{}]", token);
        OAuthRequest oReq = new OAuthRequest(Verb.GET, "https://api.linkedin.com/v1/people/~?format=json");
        oAuth2Service.signRequest(token, oReq);
        Response oResp = oAuth2Service.execute(oReq);
        LOGGER.info("Linkedin Profile: [{}]", oResp.getBody());
        resp.getOutputStream().write(oResp.getBody().getBytes(StandardCharsets.UTF_8));
    } catch (InterruptedException | ExecutionException ex) {
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(com.github.scribejava.core.model.Response) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) ExecutionException(java.util.concurrent.ExecutionException) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 65 with OAuthRequest

use of com.github.scribejava.core.model.OAuthRequest in project scribejava by scribejava.

the class MicrosoftAzureActiveDirectoryExample method main.

public static void main(String... args) throws IOException, InterruptedException, ExecutionException {
    // Replace these with your client id and secret
    final String clientId = "client id here";
    final String clientSecret = "client secret here";
    final OAuth20Service service = new ServiceBuilder(clientId).apiSecret(clientSecret).scope("openid").callback("http://www.example.com/oauth_callback/").build(MicrosoftAzureActiveDirectoryApi.instance());
    final Scanner in = new Scanner(System.in, "UTF-8");
    System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
    System.out.println();
    // Obtain the Authorization URL
    System.out.println("Fetching the Authorization URL...");
    final String authorizationUrl = service.getAuthorizationUrl();
    System.out.println("Got the Authorization URL!");
    System.out.println("Now go and authorize ScribeJava here:");
    System.out.println(authorizationUrl);
    System.out.println("And paste the authorization code here");
    System.out.print(">>");
    final String code = in.nextLine();
    System.out.println();
    // Trade the Request Token and Verfier for the Access Token
    System.out.println("Trading the Request Token for an Access Token...");
    final OAuth2AccessToken accessToken = service.getAccessToken(code);
    System.out.println("Got the Access Token!");
    System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
    System.out.println();
    // Now let's go and ask for a protected resource!
    System.out.println("Now we're going to access a protected resource...");
    final OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
    service.signRequest(accessToken, request);
    final Response response = service.execute(request);
    System.out.println("Got it! Lets see what we found...");
    System.out.println();
    System.out.println(response.getCode());
    System.out.println(response.getBody());
    System.out.println();
    System.out.println("Thats it man! Go and build something awesome with ScribeJava! :)");
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Scanner(java.util.Scanner) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder)

Aggregations

OAuthRequest (com.github.scribejava.core.model.OAuthRequest)107 Response (com.github.scribejava.core.model.Response)85 ServiceBuilder (com.github.scribejava.core.builder.ServiceBuilder)62 Scanner (java.util.Scanner)60 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)48 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)45 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)21 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)21 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)20 Random (java.util.Random)16 OAuthConfig (com.github.scribejava.core.model.OAuthConfig)13 Test (org.junit.Test)11 HttpUrl (okhttp3.HttpUrl)9 MockResponse (okhttp3.mockwebserver.MockResponse)8 MockWebServer (okhttp3.mockwebserver.MockWebServer)8 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)6 DefaultApi20 (com.github.scribejava.core.builder.api.DefaultApi20)4 ExecutionException (java.util.concurrent.ExecutionException)4