Search in sources :

Example 6 with Verifier

use of org.scribe.model.Verifier 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(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with Verifier

use of org.scribe.model.Verifier 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(com.fasterxml.jackson.databind.ObjectMapper)

Example 8 with Verifier

use of org.scribe.model.Verifier in project camel by apache.

the class YammerAccessCodeGenerator method main.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Paste the consumerKey here");
    System.out.print(">>");
    String apiKey = in.nextLine();
    System.out.println("Paste the consumerSecret here");
    System.out.print(">>");
    String apiSecret = in.nextLine();
    OAuthService service = new ServiceBuilder().provider(YammerApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
    String authorizationUrl = service.getAuthorizationUrl(EMPTY_TOKEN);
    System.out.println("Go and authorize your app here (eg. in a web browser):");
    System.out.println(authorizationUrl);
    System.out.println("... and paste the authorization code here");
    System.out.print(">>");
    Verifier verifier = new Verifier(in.nextLine());
    System.out.println();
    Token accessToken = service.getAccessToken(EMPTY_TOKEN, verifier);
    System.out.println("Your Access Token is: " + accessToken);
    System.out.println();
    in.close();
}
Also used : Scanner(java.util.Scanner) OAuthService(org.scribe.oauth.OAuthService) Token(org.scribe.model.Token) Verifier(org.scribe.model.Verifier) YammerApi(org.apache.camel.component.yammer.scribe.YammerApi) ServiceBuilder(org.scribe.builder.ServiceBuilder)

Example 9 with Verifier

use of org.scribe.model.Verifier in project data-transfer-project by google.

the class FlickrAuthDataGenerator method generateAuthData.

@Override
public AuthData generateAuthData(String callbackBaseUrl, String authCode, String id, AuthData initialAuthData, String extra) {
    Preconditions.checkArgument(Strings.isNullOrEmpty(extra), "Extra data not expected");
    Preconditions.checkNotNull(initialAuthData, "Earlier auth data not expected for Flickr flow");
    AuthInterface authInterface = flickr.getAuthInterface();
    Token token = fromAuthData(initialAuthData);
    Token requestToken = authInterface.getAccessToken(token, new Verifier(authCode));
    try {
        authInterface.checkToken(requestToken);
    } catch (FlickrException e) {
        logger.warn("Problem verifying auth token {}", e);
        return null;
    }
    return new TokenSecretAuthData(requestToken.getToken(), requestToken.getSecret());
}
Also used : AuthInterface(com.flickr4java.flickr.auth.AuthInterface) TokenSecretAuthData(org.dataportabilityproject.types.transfer.auth.TokenSecretAuthData) FlickrException(com.flickr4java.flickr.FlickrException) Token(org.scribe.model.Token) Verifier(org.scribe.model.Verifier)

Example 10 with Verifier

use of org.scribe.model.Verifier in project mamute by caelum.

the class GoogleAuthController method signUpViaGoogle.

@Get("/sign-up/google/")
public void signUpViaGoogle(String state, String code) {
    Token token = service.getAccessToken(null, new Verifier(code));
    SocialAPI googleAPI = new GoogleAPI(token, service);
    loginManager.merge(MethodType.GOOGLE, googleAPI);
    redirectToRightUrl(state);
}
Also used : GoogleAPI(org.mamute.auth.GoogleAPI) Token(org.scribe.model.Token) Verifier(org.scribe.model.Verifier) SocialAPI(org.mamute.auth.SocialAPI) Get(br.com.caelum.vraptor.Get)

Aggregations

Verifier (org.scribe.model.Verifier)14 Token (org.scribe.model.Token)13 IOException (java.io.IOException)7 OAuthRequest (org.scribe.model.OAuthRequest)4 Response (org.scribe.model.Response)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 FlickrException (com.flickr4java.flickr.FlickrException)3 AuthInterface (com.flickr4java.flickr.auth.AuthInterface)3 AuthenticationResult (fi.otavanopisto.muikku.auth.AuthenticationResult)3 Calendar (java.util.Calendar)3 Date (java.util.Date)3 GregorianCalendar (java.util.GregorianCalendar)3 HttpSession (javax.servlet.http.HttpSession)3 OAuthService (org.scribe.oauth.OAuthService)3 Uri (android.net.Uri)2 Get (br.com.caelum.vraptor.Get)2 SocialAPI (org.mamute.auth.SocialAPI)2 ServiceBuilder (org.scribe.builder.ServiceBuilder)2 Auth (com.flickr4java.flickr.auth.Auth)1 AuthenticationException (fi.otavanopisto.pyramus.plugin.auth.AuthenticationException)1