Package org.restlet.ext.oauth.internal

Examples of org.restlet.ext.oauth.internal.CookieCopyClientResource


            getLogger().info("After Redirecting to : " + redirRef.toUri());
            // return true;
            // return null;
        } else {
            getLogger().info("Came back after SNS code = " + code);
            ClientResource tokenResource = new CookieCopyClientResource(
                    params.getBaseRef() + params.getAccessTokenPath());

            Form form = new Form();
            form.add(OAuthServerResource.GRANT_TYPE,
                    OAuthServerResource.GrantType.authorization_code.name());
            String redir = request.getResourceRef().getHostIdentifier()
                    + request.getResourceRef().getPath();
            form.add(OAuthServerResource.REDIR_URI, redir);

            if (basicSecret) {
                ChallengeResponse authentication = new ChallengeResponse(
                        ChallengeScheme.HTTP_BASIC);
                authentication.setDigestAlgorithm("NONE");
                String basic = params.getClientId() + ':'
                        + params.getClientSecret();
                authentication.setRawValue(Base64.encode(basic.getBytes(),
                        false));
                tokenResource.setChallengeResponse(authentication);
            } else {
                form.add(OAuthServerResource.CLIENT_ID, params.getClientId());
                form.add(OAuthServerResource.CLIENT_SECRET,
                        params.getClientSecret());
            }

            form.add(OAuthServerResource.CODE, code);
            getLogger().info(
                    "Sending access form : " + form.getQueryString() + " to : "
                            + tokenResource.getReference());

            try {
                Representation input = form.getWebRepresentation();
                Representation body = tokenResource.post(input);

                if (tokenResource.getStatus().isSuccess()) {
                    // Store away the user
                    OAuthUser authUser = OAuthUser.createJson(body);

                    if (authUser != null) {
                        request.getClientInfo().setUser(authUser);
                        request.getClientInfo().setAuthenticated(true);
                        getLogger().info(
                                "storing to context = : " + getContext());
                        // continue in the filter chain
                        auth = true;
                    }
                }

                getLogger().info("Before sns release");
                body.release();
            } catch (ResourceException re) {
                getLogger().warning("Could not find token resource.");
            }
            tokenResource.release();
        }
        return auth;
    }
View Full Code Here


            cr.setParameters(parameters);
            resp.getChallengeRequests().add(cr);
            resp.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        } else {
            getLogger().info("Found Access Token " + accessToken);
            ClientResource authResource = new CookieCopyClientResource(
                    validateRef);

            JSONObject request;
            try {
                request = createValidationRequest(accessToken, req);
                // Representation repr = this.createJsonRepresentation(request);
                Representation repr = new JsonStringRepresentation(request);
                getLogger().info("Posting to validator... json = " + request);
                // RETRIEVE JSON...WORKAROUND TO HANDLE ANDROID
                Representation r = authResource.post(repr);
                getLogger().info("After posting to validator...");
                repr.release();
                getLogger().info(
                        "Got Respose from auth resource OK "
                                + r.getClass().getCanonicalName());
                JsonRepresentation returned = new JsonRepresentation(r);

                // GET OBJECT
                JSONObject response = returned.getJsonObject();
                boolean authenticated = response.getBoolean("authenticated");

                if (response.has("tokenOwner"))
                    this.setUser(req, response, accessToken);

                String error = null;
                if (response.has("error"))
                    error = response.getString("error");

                getLogger().info("In Auth Filer -> " + authenticated);

                // Clean-up
                returned.release();
                r.release();
                authResource.release();

                if (authenticated)
                    return true;

                // handle any errors:
                handleError(error, resp);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (authResource != null) {
                    authResource.getResponse().release();
                    authResource.release();
                }
            }
        }

        return false;
View Full Code Here

        form.add("pass", fbPass);

        String q = form.getQueryString();
        Reference redirRef = new Reference(params.getBaseRef(),
                params.getAuthorizePath(), q, null);
        ClientResource authResource = new CookieCopyClientResource(
                redirRef.toUri());
        authResource.setFollowingRedirects(false); // token is in a 3xx
        Representation r = authResource.get();

        int maxRedirCnt = 10; // Stop the maddness if out of hand...
        int cnt = 0;

        while (authResource.getStatus().isRedirection()) {
            String fragment = authResource.getLocationRef().getFragment();
            if (fragment != null && fragment.length() > 0) {
                Form f = new Form(fragment);

                String accessToken = f
                        .getFirstValue(OAuthServerResource.ACCESS_TOKEN);

                String refreshToken = f
                        .getFirstValue(OAuthServerResource.REFRESH_TOKEN);

                long expiresIn = 0;
                String exp = f.getFirstValue(OAuthServerResource.EXPIRES_IN);
                if (exp != null && exp.length() > 0) {
                    expiresIn = Long.parseLong(exp);
                }

                if (accessToken != null && accessToken.length() > 0) {
                    Context.getCurrentLogger().info(
                            "Successful UserAgent flow : AccessToken = "
                                    + accessToken + " RefreshToken = "
                                    + refreshToken + " ExpiresIn = "
                                    + expiresIn);
                    break;
                }
            }

            if (++cnt >= maxRedirCnt)
                break;

            Context.getCurrentLogger().info(
                    "Redir to = " + authResource.getLocationRef());
            authResource.setReference(authResource.getLocationRef());
            authResource.get();
        }

        if (authResource.getStatus().isSuccess()) {
            result = authResource.getCookieSettings();
        }

        r.release();
        authResource.release();

        return result;
    }
View Full Code Here

TOP

Related Classes of org.restlet.ext.oauth.internal.CookieCopyClientResource

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.