Search in sources :

Example 11 with OAuthRequest

use of org.scribe.model.OAuthRequest in project muikku by otavanopisto.

the class GoogleAuthenticationStrategy method processResponse.

@Override
protected AuthenticationResult processResponse(AuthSource authSource, Map<String, String[]> requestParameters, OAuthService service, String[] requestedScopes) {
    ObjectMapper objectMapper = new ObjectMapper();
    String verifier = getFirstRequestParameter(requestParameters, "code");
    Verifier v = new Verifier(verifier);
    Token accessToken = service.getAccessToken(null, v);
    GoogleAccessToken googleAccessToken;
    try {
        googleAccessToken = objectMapper.readValue(accessToken.getRawResponse(), GoogleAccessToken.class);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, googleAccessToken.getExpiresIn());
        Date expires = calendar.getTime();
        sessionController.addOAuthAccessToken("google", expires, accessToken.getToken(), null);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Token extraction failed a JSON parsing error", e);
        return new AuthenticationResult(AuthenticationResult.Status.ERROR);
    }
    List<String> scopesList = Arrays.asList(requestedScopes);
    boolean hasProfileScope = scopesList.contains("https://www.googleapis.com/auth/userinfo.profile");
    GoogleUserInfo userInfo = null;
    if (hasProfileScope) {
        OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
        service.signRequest(accessToken, request);
        Response response = request.send();
        try {
            userInfo = objectMapper.readValue(response.getBody(), GoogleUserInfo.class);
        } catch (IOException e) {
            logger.log(Level.SEVERE, "Logging in failed because of a JSON parsing exception", e);
            return new AuthenticationResult(AuthenticationResult.Status.ERROR);
        }
    }
    if (userInfo != null)
        return processLogin(authSource, requestParameters, userInfo.getId(), Arrays.asList(userInfo.getEmail()), userInfo.getGivenName(), userInfo.getFamilyName());
    else {
        return new AuthenticationResult(AuthenticationResult.Status.GRANT);
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Token(org.scribe.model.Token) IOException(java.io.IOException) Verifier(org.scribe.model.Verifier) Date(java.util.Date) AuthenticationResult(fi.otavanopisto.muikku.auth.AuthenticationResult) Response(org.scribe.model.Response) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 12 with OAuthRequest

use of org.scribe.model.OAuthRequest in project muikku by otavanopisto.

the class GoogleApi20ServiceImpl method getAccessToken.

@Override
public Token getAccessToken(Token requestToken, Verifier verifier) {
    OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
    request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
    request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
    request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
    request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
    request.addBodyParameter("grant_type", "authorization_code");
    if (config.hasScope())
        request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
    Response response = request.send();
    ObjectMapper objectMapper = new ObjectMapper();
    String tokenJson;
    try {
        tokenJson = objectMapper.writeValueAsString(objectMapper.readTree(response.getBody()));
    } catch (IOException e) {
        throw new OAuthException("Invalid Token JSON", e);
    }
    return api.getAccessTokenExtractor().extract(tokenJson);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) OAuthException(org.scribe.exceptions.OAuthException) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 13 with OAuthRequest

use of org.scribe.model.OAuthRequest in project muikku by otavanopisto.

the class FacebookAuthenticationStrategy method processResponse.

@Override
protected AuthenticationResult processResponse(AuthSource authSource, Map<String, String[]> requestParameters, OAuthService service, String[] requestedScopes) {
    ObjectMapper objectMapper = new ObjectMapper();
    String verifier = getFirstRequestParameter(requestParameters, "code");
    Verifier v = new Verifier(verifier);
    Token accessToken = service.getAccessToken(null, v);
    FacebookUser meObject = null;
    OAuthRequest request = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me");
    service.signRequest(accessToken, request);
    Response response = request.send();
    try {
        meObject = objectMapper.readValue(response.getBody(), FacebookUser.class);
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Logging in failed because of a JSON parsing exception", e);
        return new AuthenticationResult(AuthenticationResult.Status.ERROR);
    }
    Integer expiresIn = extractExpires(accessToken);
    Date expires = null;
    if (expiresIn != null) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.SECOND, expiresIn);
        expires = calendar.getTime();
        sessionController.addOAuthAccessToken("facebook", expires, accessToken.getToken(), null);
    }
    if (meObject != null)
        return processLogin(authSource, requestParameters, meObject.getId(), Arrays.asList(meObject.getEmail()), meObject.getFirstName(), meObject.getLastName());
    else {
        return new AuthenticationResult(AuthenticationResult.Status.GRANT);
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Token(org.scribe.model.Token) IOException(java.io.IOException) Verifier(org.scribe.model.Verifier) Date(java.util.Date) AuthenticationResult(fi.otavanopisto.muikku.auth.AuthenticationResult) Response(org.scribe.model.Response) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 14 with OAuthRequest

use of org.scribe.model.OAuthRequest in project openolat by klemens.

the class FacebookProvider method getUser.

@Override
public OAuthUser getUser(OAuthService service, Token accessToken) {
    OAuthRequest request = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me");
    service.signRequest(accessToken, request);
    Response oauthResponse = request.send();
    String body = oauthResponse.getBody();
    return parseInfos(body);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response)

Example 15 with OAuthRequest

use of org.scribe.model.OAuthRequest in project fitscales by paulburton.

the class FitBitSyncService method syncWeight.

@Override
public boolean syncWeight(float weight) {
    try {
        SimpleDateFormat tsFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        String timestamp = tsFormat.format(new Date());
        OAuthRequest request = new OAuthRequest(Verb.POST, API_BASE + "/user/-/body.json");
        request.addBodyParameter("weight", String.format("%.2f", weight));
        request.addBodyParameter("date", timestamp);
        oaService.signRequest(oaToken, request);
        Response response = request.send();
        int code = response.getCode();
        if (DEBUG) {
            Log.d(TAG, "Response code " + code);
            try {
                Log.d(TAG, "Response body " + response.getBody());
            } catch (Exception ex) {
                Log.e(TAG, "Response body error", ex);
            }
        }
        if (code == 200 || code == 201 || code == 204)
            return true;
        return false;
    } catch (Exception ex) {
        return false;
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Aggregations

OAuthRequest (org.scribe.model.OAuthRequest)25 Response (org.scribe.model.Response)24 IOException (java.io.IOException)6 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 Date (java.util.Date)5 Token (org.scribe.model.Token)4 Verifier (org.scribe.model.Verifier)4 AuthenticationResult (fi.otavanopisto.muikku.auth.AuthenticationResult)3 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 JSONObject (org.json.JSONObject)3 OAuthException (org.scribe.exceptions.OAuthException)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Contributor (ai.elimu.model.Contributor)1 SignOnEvent (ai.elimu.model.contributor.SignOnEvent)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 WhoAmI (fi.otavanopisto.pyramus.rest.model.WhoAmI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1