Package org.restlet.data

Examples of org.restlet.data.ChallengeRequest


     * @param header
     *            The HTTP header value to parse.
     * @return The parsed challenge request.
     */
    public static ChallengeRequest parseRequest(String header) {
        ChallengeRequest result = null;

        if (header != null) {
            int space = header.indexOf(' ');

            if (space != -1) {
                String scheme = header.substring(0, space);
                String realm = header.substring(space + 1);
                int equals = realm.indexOf('=');
                String realmValue = realm.substring(equals + 2,
                        realm.length() - 1);
                result = new ChallengeRequest(new ChallengeScheme("HTTP_"
                        + scheme, scheme), realmValue);
            }
        }

        return result;
View Full Code Here


        this(context, false, realm, null, serverKey);
    }

    @Override
    protected ChallengeRequest createChallengeRequest(boolean stale) {
        ChallengeRequest result = super.createChallengeRequest(stale);
        result.setDomainRefs(getDomainRefs());
        result.setStale(stale);
        result.setServerNonce(generateServerNonce());
        return result;
    }
View Full Code Here

     * @param stale
     *            Indicates if the new challenge is due to a stale response.
     * @return A new challenge request.
     */
    protected ChallengeRequest createChallengeRequest(boolean stale) {
        return new ChallengeRequest(getScheme(), getRealm());
    }
View Full Code Here

     * @param response
     *            The response.
     */
    public static void update(ChallengeResponse challengeResponse,
            Request request, Response response) {
        ChallengeRequest challengeRequest = null;
        for (ChallengeRequest c : response.getChallengeRequests()) {
            if (challengeResponse.getScheme().equals(c.getScheme())) {
                challengeRequest = c;
                break;
            }
        }

        String realm = null;
        String nonce = null;
        if (challengeRequest != null) {
            realm = challengeRequest.getRealm();
            nonce = challengeRequest.getServerNonce();
            challengeResponse.setOpaque(challengeRequest.getOpaque());
        }
        challengeResponse.setRealm(realm);
        challengeResponse.setServerNonce(nonce);

        challengeResponse.setDigestRef(new Reference(request.getResourceRef()
View Full Code Here

    public boolean authorize(Request req, Response resp) {
        getLogger().info("Checking for param access_token");
        String accessToken = getAccessToken(req);

        if (accessToken == null || accessToken.length() == 0) {
            ChallengeRequest cr = new ChallengeRequest(
                    ChallengeScheme.HTTP_OAUTH, "oauth"); // TODO set realm
            Series<Parameter> parameters = new Form();
            parameters.add("error", OAuthError.INVALID_REQUEST.name());
            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(
View Full Code Here

     * @param resp
     *            The response to complete.
     */
    private void handleError(String error, Response resp) {
        if (error != null && error.length() > 0) {
            ChallengeRequest cr = new ChallengeRequest(
                    ChallengeScheme.HTTP_OAUTH, "oauth"); // TODO set
            // realm
            Series<Parameter> parameters = new Form();
            parameters.add("error", error);
            OAuthError code = OAuthError.valueOf(error);

            switch (code) {
            case INVALID_REQUEST:
                // TODO report bug in Restlet and verify, can not handle
                // space char.
                // parameters.add("error_description",
                // "The request is missing a required parameter.");
                resp.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                break;
            case INVALID_TOKEN:
            case EXPIRED_TOKEN:
                // parameters.add("error_description",
                // "The access token provided is invalid.");
                resp.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
                break;
            case INSUFFICIENT_SCOPE:
                // parameters.add("error_description",
                // "The request requires higher privileges than provided "
                // +"by the access token.");
                resp.setStatus(Status.CLIENT_ERROR_FORBIDDEN);
                break;

            }

            // parameters.add("error_uri",authorizeRef.toString());
            cr.setParameters(parameters);
            resp.getChallengeRequests().add(cr);
        }
    }
View Full Code Here

        try {
            cr.get();
        } catch (ResourceException re) {
            assertEquals(Status.CLIENT_ERROR_UNAUTHORIZED, cr.getStatus());

            ChallengeRequest c1 = null;
            for (ChallengeRequest challengeRequest : cr.getChallengeRequests()) {
                if (ChallengeScheme.HTTP_DIGEST.equals(challengeRequest
                        .getScheme())) {
                    c1 = challengeRequest;
                    break;
                }
            }
            assertEquals(ChallengeScheme.HTTP_DIGEST, c1.getScheme());

            String realm = c1.getRealm();
            assertEquals("TestRealm", realm);

            // String opaque = c1.getParameters().getFirstValue("opaque");
            // String qop = c1.getParameters().getFirstValue("qop");
            // assertEquals(null, opaque);
View Full Code Here

     * Updates the response to challenge the client for credentials.
     */
    public void challenge() {
        getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
        getResponse().getChallengeRequests().add(
                new ChallengeRequest(ChallengeScheme.HTTP_BASIC, "Restlet"));
    }
View Full Code Here

        super(header);
    }

    @Override
    public ChallengeRequest readValue() throws IOException {
        ChallengeRequest result = null;

        // The challenge is that this header is a comma separated lst of
        // challenges, and that each challenges is also a comma separated list,
        // but of parameters.
        skipSpaces();
        if (peek() != -1) {
            String scheme = readToken();
            result = new ChallengeRequest(new ChallengeScheme("HTTP_" + scheme,
                    scheme));
            skipSpaces();

            // Header writer that will reconstruct the raw value of a challenge.
            HeaderWriter<Parameter> w = new HeaderWriter<Parameter>() {
                @Override
                public HeaderWriter<Parameter> append(Parameter value) {
                    appendExtension(value);
                    return this;
                }
            };

            boolean stop = false;
            while (peek() != -1 && !stop) {
                boolean sepSkipped = skipValueSeparator();
                // Record the start of the segment
                mark();
                // Read a token and the next character.
                readToken();
                int nextChar = read();
                reset();
                if (isSpace(nextChar)) {
                    // A new scheme has been discovered.
                    stop = true;
                } else {
                    // The next segment is considered as a parameter
                    if (sepSkipped) {
                        // Add the skipped value separator.
                        w.appendValueSeparator();
                    }
                    // Append the parameter
                    w.append(readParameter());
                }
            }
            result.setRawValue(w.toString());
        }

        return result;
    }
View Full Code Here

               } catch (IllegalArgumentException ex) {
                  getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                  return null;
               }
            }
            getResponse().setChallengeRequests(Collections.singletonList(new ChallengeRequest(ChallengeScheme.HTTP_BASIC,realm==null ? "users" : "realm "+realm.getName())));
            getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
            return null;
         }
         String authid = authorization.getIdentifier().trim();
         String password = new String(authorization.getSecret());
         User user = null;
         if (realm!=null) {
            RealmUser ruser = findRealmUser(db,realm,authid);
            if (ruser!=null) {
               user = ruser.getUser();
            }
            if (user==null) {
               // see if the user is a super user across realms
               user = findUser(db,authid);
               if (user!=null) {
                  // The user must either be a superuser or have the cross-realm permission
                  Permission superuser = db.getPermission(AuthDB.SUPERUSER_PERMISSION);
                  Permission crossrealm = db.getPermission(AuthDB.ACROSS_REALM_PERMISSION);
                  if (!user.hasPermission(superuser) && !user.hasPermission(crossrealm)) {
                     user = null;
                  }
               }
            }
         } else {
            user = findUser(db,authid);
         }
         if (user!=null) {
            try {
               String seconds = requestForm.getFirstValue("expiration");
               long requestExpiration = seconds==null ? expiration : Long.parseLong(seconds)*1000;
               if ("false".equals(requestForm.getFirstValue("session"))) {
                  requestExpiration = 0;
               }
               User.Authenticated authd = user.authenticate(realm,password,requestExpiration);
               if (authd!=null) {
                  String userAlias = authd.getUser().getAlias();
                  UUID userId = authd.getUser().getUUID();
                  String name = authd.getUser().getName();
                  String email = authd.getUser().getEmail();
                  Iterator<Group> groups = null;
                  Iterator<Role> roles = authd.getUser().getRoles();
                  // If we have a realm, we need the realm user to get the right alias
                  if (realm!=null) {
                     RealmUser ruser = db.findRealmUser(realm,authd.getUser());
                     if (ruser!=null) {
                        userAlias = ruser.getAlias();
                        name = ruser.getName();
                        email = ruser.getEmail();
                        groups = ruser.getGroups();
                     }
                  }
                  getResponse().setStatus(Status.SUCCESS_OK);
                  return getSessionEntity(authd.getSession(),userId,userAlias,name,email,roles,groups);
               }
            } catch (Exception ex) {
               getContext().getLogger().log(Level.SEVERE,"Failed to authenticate due to exception.",ex);
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
               return null;
            }
         }
         getResponse().setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
         getResponse().setChallengeRequests(Collections.singletonList(new ChallengeRequest(ChallengeScheme.HTTP_BASIC,realm==null ? "Realm Users" : "Users")));
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get user data from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
      }
      return null;
View Full Code Here

TOP

Related Classes of org.restlet.data.ChallengeRequest

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.