Package org.scribe.oauth

Examples of org.scribe.oauth.OAuthService


        String state = RandomStringUtils.randomAlphabetic(10);
        String key = RandomStringUtils.randomAlphabetic(10);
        String secret = RandomStringUtils.randomAlphabetic(10);
        LinkedInApi20 api = new LinkedInApi20(state);

        OAuthService service = new ServiceBuilder().provider(api).apiKey(key).apiSecret(secret)
                .callback("http://localhost:4502/linkedin").build();

        String expected = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=" + key
                + "&state=" + state + "&redirect_uri=" + OAuthEncoder.encode("http://localhost:4502/linkedin");

        assertEquals(expected, service.getAuthorizationUrl(null));
    }
View Full Code Here


        String state = RandomStringUtils.randomAlphabetic(10);
        String key = RandomStringUtils.randomAlphabetic(10);
        String secret = RandomStringUtils.randomAlphabetic(10);
        LinkedInApi20 api = new LinkedInApi20(state);

        OAuthService service = new ServiceBuilder().provider(api).apiKey(key).apiSecret(secret)
                .callback("http://localhost:4502/linkedin").scope("r_basicprofile,r_emailaddress")
                .build();

        String expected = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=" + key
                + "&state=" + state + "&redirect_uri=" + OAuthEncoder.encode("http://localhost:4502/linkedin")
                + "&scope=" + "r_basicprofile%2Cr_emailaddress";

        assertEquals(expected, service.getAuthorizationUrl(null));
    }
View Full Code Here

            httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        Verifier verifier = new Verifier(inboundVerifier);
        OAuthService service = getOAuthService(httpRequest, false);

        // TODO: Store this token if authorization succeeds
        Token accessToken = service.getAccessToken(storedToken, verifier);

        OAuthRequest authRequest = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
        service.signRequest(accessToken, authRequest);
        Response authResponse = authRequest.send();

        if (!authResponse.isSuccessful()) {
            LOGGER.warn("OAuth handshake completed, but Twitter credential verification failed: " + authResponse.getMessage());
            httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
View Full Code Here

            login(httpRequest, httpResponse, chain);
        }
    }

    private void login(HttpServletRequest httpRequest, HttpServletResponse httpResponse, HandlerChain chain) throws IOException {
        OAuthService service = getOAuthService(httpRequest, true);
        Token requestToken = service.getRequestToken();
        httpRequest.getSession().setAttribute(OAUTH_REQUEST_TOKEN, requestToken);
        String authUrl = service.getAuthorizationUrl(requestToken);
        httpResponse.sendRedirect(authUrl);
    }
View Full Code Here

      @RequestParam String target, WebRequest request) {
    Site site = (Site) request.getAttribute(AttributeKeys.SITE_KEY, WebRequest.SCOPE_REQUEST);
    String callback = site.getHomeURL()+ "/oauth/callback/" + target;
    String beanId = target + ApiService.class.getSimpleName();
    ApiService apiService = appContext.getBean(beanId, ApiService.class);
    OAuthService oAuthService = apiService.getOAuthService(callback);
    Token requestToken = oAuthService.getRequestToken();
    oAuthServices.put(requestToken, oAuthService);
    requestTokens.put(requestToken.getToken(), requestToken);
    String authUrl = oAuthService.getAuthorizationUrl(requestToken);
    Map<String, Object> model = CollectionUtil.newHashMap();
    model.put("authUrl", authUrl);
    return model;
  }
View Full Code Here

  public ResponseEntity<String> callback(@PathVariable String target,
      @RequestParam String oauth_token, @RequestParam String oauth_verifier, HttpSession session) {
    Long userId = (Long) session.getAttribute(AttributeKeys.USER_ID_KEY);
    Token requestToken = requestTokens.get(oauth_token);
    Verifier verifier = new Verifier(oauth_verifier);
    OAuthService oAuthService = oAuthServices.get(requestToken);
    Token accessToken = oAuthService.getAccessToken(requestToken, verifier);
    Authorization authorization = new Authorization(
                    target,
                    accessToken.getToken(),
                    accessToken.getSecret(),
                    userId);
View Full Code Here

    private final static Log logger=LogFactory .getLog(OAuthLoginServiceImpl.class);

    private OAuthService getOAuthService(int authProvider) throws OurException
    {
        OAuthService service = null;
        switch(authProvider)
        {
            case ClientUtils.FACEBOOK:
            {
                service = new ServiceBuilder()
View Full Code Here

        String authorizationUrl = null;
        Token requestToken = null;
       
        int authProvider = credential.getAuthProvider();
       
        OAuthService service = getOAuthService(authProvider);
        if (service == null)
        {
            throw new OurException("Could not build OAuthService");
        }
       
       
        if (authProvider == ClientUtils.TWITTER    ||
            authProvider == ClientUtils.YAHOO      ||
            authProvider == ClientUtils.LINKEDIN   ||
            authProvider == ClientUtils.FLICKR     ||
            authProvider == ClientUtils.IMGUR      ||
            authProvider == ClientUtils.TUMBLR     ||
            authProvider == ClientUtils.VIMEO)
        {
            String authProviderName = ClientUtils.getAuthProviderName(authProvider);
            logger.info(authProviderName + " requires Request token first.. obtaining..");
            try
            {
                requestToken = service.getRequestToken();
                logger.info("Got request token: " + requestToken);
                // we must save in the session. It will be required to
                // get the access token
                saveRequestTokenToSession(requestToken);
            }
            catch(Exception e)
            {
                String stackTrace = stackTraceToString(e);
                throw new OurException("Could not get request token for " + authProvider + " " + stackTrace);
            }
           
        }
        logger.info("Getting Authorization url...");
        try
        {
            authorizationUrl = service.getAuthorizationUrl(requestToken);
           
            // Facebook has optional state var to protect against CSFR.
            // We'll use it
            if (authProvider == ClientUtils.FACEBOOK ||
                authProvider == ClientUtils.GITHUB   ||
View Full Code Here

    }
   
    private SocialUser getSocialUser(Token accessToken,int authProvider) throws OurException
    {
        logger.info("Token: " + accessToken + " Provider: " + authProvider);
        OAuthService service = getOAuthService(authProvider);
       
       
        String url = getProtectedResourceUrlFromSession();
        OAuthRequest request = new OAuthRequest(Verb.GET,url);
        // sign the request
        service.signRequest(accessToken,request);
        Response response = request.send();
        String json = response.getBody();
        SocialUser socialUser = getSocialUserFromJson(json,authProvider);
        return socialUser;
    }
View Full Code Here

        */
       
        /* if there is any request token in session, get it */
        requestToken = getRequestTokenFromSession();
       
        OAuthService service = null;
        Verifier verifier    = null;
        Token accessToken    = null;
       
        /* Get Access Token */
        if (authProvider != ClientUtils.DEFAULT)
        {
            service = getOAuthService(authProvider);
            verifier = new Verifier(credential.getVerifier());
            logger.info("Requesting access token with requestToken: " + requestToken);
            logger.info("verifier=" + verifier);
            try
            {
                accessToken = service.getAccessToken(requestToken,verifier);
                if (accessToken == null)
                {
                    logger.error("Could not get Access Token for " + authProviderName);
                    throw new OurException("Could not get Access Token");
                }
            }
            catch (Exception e)
            {
                logger.info("Exception received gettign Access Token: " + e);
                throw new OurException("Exception received getting Access Token: " + e);
            }
            logger.info("Got the access token: " + accessToken);
            logger.info(" Token: " + accessToken.getToken());
            logger.info(" Secret: " + accessToken.getSecret());
            logger.info(" Raw: " + accessToken.getRawResponse());
        }
        else
        {
            /*
            ** Default provider.
            ** The info will probably come from database. Password will
            ** probably some kind of salted hash. We're just hard coding
            ** "test" and "secret" for the demo.
            */
            logger.info("Handing default loign..");
            String username = credential.getLoginName();
            String password = credential.getPassword();
            if (username == null)
            {
                throw new OurException("Default Username can not be empty");
            }
            if (password == null)
            {
                throw new OurException("Default Password not be empty");
            }
            if (username.equals(getDefaultUsername()) && password.equals(getDefaultPassword()))
            {
               
            }
            else
            {
                throw new OurException("Please use " + getDefaultUsername() + "  and " + getDefaultPassword() + " as Default Credential!");
            }
           
           
        }
       
        if (authProvider == ClientUtils.INSTAGRAM)
        {
            try
            {
                instragramToken = InstragramToken.parse(accessToken.getRawResponse());
            } catch (ParseException e)
            {
                throw new OurException("Could not parse " + authProviderName + " Json AccessToken");
            }
            logger.info("Getting Instragram Access Token");
            logger.info(" access token" + instragramToken.getAcessToken());
            logger.info(" userId: " + instragramToken.getUserId());
            logger.info(" full name: " + instragramToken.getFullName());
            logger.info(" username: " + instragramToken.getFullName());
            logger.info(" raw: " + instragramToken.getRawResponse());
           
            // replace userId and access token in protected resource url
            protectedResourceUrl = ClientUtils.getProctedResourceUrl(authProvider);
            logger.info("Instragram protected resource url: " + protectedResourceUrl);
            protectedResourceUrl = String.format(protectedResourceUrl, instragramToken.getUserId(),instragramToken.getAcessToken());
            logger.info("Instragram protected resource url: " + protectedResourceUrl);
        }
       
        if (authProvider == ClientUtils.GITHUB ||
            authProvider == ClientUtils.FOURSQUARE)
        {
            protectedResourceUrl = String.format(protectedResourceUrl,accessToken.getToken());
        }
       
       
        if (authProvider == ClientUtils.YAHOO)
        {
            //throw new OurException("Not implemented for yahoo yet!)");
            /* we need to replace <GUID> */
            yahooGuid = getQueryStringValue(accessToken.getRawResponse(),"xoauth_yahoo_guid");
            if (yahooGuid == null)
            {
                throw new OurException("Could not get Yahoo GUID from Query String");
            }
            // must save it to session. we'll use to get the user profile
            saveYahooGuidToSession(yahooGuid);
           
            protectedResourceUrl = ClientUtils.getProctedResourceUrl(authProvider);
            protectedResourceUrl = String.format(protectedResourceUrl,yahooGuid);
            logger.info("Yahoo protected resource url: " + protectedResourceUrl);
                   
        }
       
        // make session id
        String sessionId = makeRandomString();
       
        // must save session id to session
        saveSessionIdToSession(sessionId);
       
        // must save authProvider to session
        saveAuthProviderToSession(authProvider);
       
        SocialUser socialUser = null;
        if (authProvider != ClientUtils.DEFAULT)
        {
            // must save acess token to session
            saveAccessTokenToSession(accessToken);
           
            // must save the protected resource url to session
            saveProtectedResourceUrlToSession(protectedResourceUrl);
           
            // now request protected resource
            logger.info("Getting protected resource");
            logger.info("Protected resource url: " + protectedResourceUrl);
            try
            {
                OAuthRequest request = new OAuthRequest(Verb.GET,protectedResourceUrl);
                service.signRequest(accessToken,request);
               
                Response response = request.send();
                logger.info("Status code: " + response.getCode());
                logger.info("Body: " + response.getBody());
               
View Full Code Here

TOP

Related Classes of org.scribe.oauth.OAuthService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.