コード例 #1
0
  @Post
  public Representation token(final Representation entity) throws OAuth2RestletException {

    final OAuth2Request request = requestFactory.create(getRequest());

    try {
      final AccessToken accessToken = accessTokenService.requestAccessToken(request);
      return new JacksonRepresentation<Map<String, Object>>(accessToken.toMap());
    } catch (InvalidGrantException e) {
      throw new OAuth2RestletException(
          e.getStatusCode(),
          e.getError(),
          "Assertion is invalid.",
          request.<String>getParameter("redirect_uri"),
          request.<String>getParameter("state"));
    } catch (ClientAuthenticationFailedException e) {
      getResponse()
          .setChallengeRequests(
              singletonList(
                  new ChallengeRequest(
                      ChallengeScheme.valueOf(
                          SUPPORTED_RESTLET_CHALLENGE_SCHEMES.get(e.getChallengeScheme())),
                      e.getChallengeRealm())));
      throw new OAuth2RestletException(
          e.getStatusCode(), e.getError(), e.getMessage(), request.<String>getParameter("state"));
    } catch (OAuth2Exception e) {
      throw new OAuth2RestletException(
          e.getStatusCode(),
          e.getError(),
          e.getMessage(),
          request.<String>getParameter("redirect_uri"),
          request.<String>getParameter("state"));
    }
  }
コード例 #2
0
  /**
   * Returns the authentication data sent by client to the target according to the a list of
   * properties. By default, this method returns checks the variable "challengeScheme", "login",
   * "password" in order to build the ChallengeResponse object. It can be overriden.
   *
   * @param resolver The resolver.
   * @return The target challengeResponse object.
   * @throws ResourceException
   */
  protected ChallengeResponse getTargetChallengeResponse(Resolver<String> resolver)
      throws ResourceException {
    final ChallengeScheme challengeScheme =
        ChallengeScheme.valueOf(resolver.resolve("challengeScheme"));
    final String login = resolver.resolve("login");
    final String password = resolver.resolve("password");

    ChallengeResponse result = null;
    if ((challengeScheme != null) && (login != null) && (password != null)) {
      result = new ChallengeResponse(challengeScheme, login, password);
    }

    return result;
  }
コード例 #3
0
  private static Resource getResource(
      CollectInfo collectInfo, Object restlet, String basePath, ChallengeScheme scheme) {
    Resource resource = new Resource();
    resource.setResourcePath(basePath);

    if (restlet instanceof Directory) {
      Directory directory = (Directory) restlet;
      resource.setName(directory.getName());
      resource.setDescription(directory.getDescription());
    }
    if (restlet instanceof ServerResource) {
      ServerResource serverResource = (ServerResource) restlet;
      resource.setName(serverResource.getName());
      resource.setDescription(serverResource.getDescription());
    }
    if (restlet instanceof DocumentedResource) {
      DocumentedResource documentedServerResource = (DocumentedResource) restlet;
      resource.setSections(documentedServerResource.getSections());
    } else if (collectInfo.isUseSectionNamingPackageStrategy()) {
      String sectionName = restlet.getClass().getPackage().getName();
      resource.getSections().add(sectionName);
    }

    if (StringUtils.isNullOrEmpty(resource.getName())) {
      String name = restlet.getClass().getSimpleName();
      if (name.endsWith(SUFFIX_SERVER_RESOURCE)
          && name.length() > SUFFIX_SERVER_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_SERVER_RESOURCE.length());
      }
      if (name.endsWith(SUFFIX_RESOURCE) && name.length() > SUFFIX_RESOURCE.length()) {
        name = name.substring(0, name.length() - SUFFIX_RESOURCE.length());
      }
      resource.setName(name);
    }

    Template template = new Template(basePath);
    for (String variable : template.getVariableNames()) {
      PathVariable pathVariable = new PathVariable();
      pathVariable.setName(variable);
      resource.getPathVariables().add(pathVariable);
    }

    if (scheme != null) {
      resource.setAuthenticationProtocol(scheme.getName());
    }

    return resource;
  }