Ejemplo n.º 1
0
 @Path("login")
 @POST
 public String login(
     @FormParam("userName") String username,
     @FormParam("password") String password,
     @Context SecurityContext securityContext) {
   System.out.println(securityContext.isSecure());
   System.out.println(securityContext.getUserPrincipal());
   try {
     String token = new MobileLoginDAO().loginCheckDAO(username, encodePassword(password));
     if (token.equals("false")) {
       return "false";
     } else {
       return token;
     }
   } catch (Exception e) {
     System.out.println("Error: " + e);
     return "false";
   }
 }
  @Override
  public Response requestEndSession(
      String idTokenHint,
      String postLogoutRedirectUri,
      String state,
      String sessionId,
      HttpServletRequest httpRequest,
      HttpServletResponse httpResponse,
      SecurityContext sec) {

    log.debug(
        "Attempting to end session, idTokenHint: {0}, postLogoutRedirectUri: {1}, sessionId: {2}, Is Secure = {3}",
        idTokenHint, postLogoutRedirectUri, sessionId, sec.isSecure());

    EndSessionParamsValidator.validateParams(
        idTokenHint, postLogoutRedirectUri, errorResponseFactory);

    final Pair<SessionId, AuthorizationGrant> pair =
        endSession(idTokenHint, sessionId, httpRequest, httpResponse, sec);

    // Validate redirectUri
    String redirectUri =
        redirectionUriService.validatePostLogoutRedirectUri(
            pair.getSecond().getClient().getClientId(), postLogoutRedirectUri);

    if (StringUtils.isNotBlank(redirectUri)) {
      RedirectUri redirectUriResponse = new RedirectUri(redirectUri);
      if (StringUtils.isNotBlank(state)) {
        redirectUriResponse.addResponseParameter(EndSessionResponseParam.STATE, state);
      }

      return RedirectUtil.getRedirectResponseBuilder(redirectUriResponse, httpRequest).build();
    } else {
      errorResponseFactory.throwBadRequestException(EndSessionErrorResponseType.INVALID_REQUEST);
    }
    return Response.ok().build();
  }
  @Override
  public Response requestEndSessionPage(
      @ApiParam(
              value =
                  "Previously issued ID Token (id_token) passed to the logout endpoint as a hint about the End-User's current authenticated session with the Client. This is used as an indication of the identity of the End-User that the RP is requesting be logged out by the OP. The OP need not be listed as an audience of the ID Token when it is used as an id_token_hint value.",
              required = true)
          String idTokenHint,
      @ApiParam(value = "Session ID", required = false) String sessionId,
      @Context HttpServletRequest httpRequest,
      @Context HttpServletResponse httpResponse,
      @Context SecurityContext sec) {

    log.debug(
        "Attempting to end session, idTokenHint: {0}, sessionId: {1}, Is Secure = {2}",
        idTokenHint, sessionId, sec.isSecure());

    Pair<SessionId, AuthorizationGrant> pair =
        endSession(idTokenHint, sessionId, httpRequest, httpResponse, sec);

    final Set<String> logoutUris = getRpLogoutUris(pair.getFirst());
    final String html = constructPage(logoutUris);
    log.debug("Constructed http logout page: " + html);
    return Response.ok().type(MediaType.TEXT_HTML_TYPE).entity(html).build();
  }
  @Override
  public Response requestEndSession(
      String idTokenHint,
      String postLogoutRedirectUri,
      String state,
      String sessionId,
      HttpServletRequest httpRequest,
      HttpServletResponse httpResponse,
      SecurityContext sec) {
    log.debug(
        "Attempting to end session, idTokenHint: {0}, postLogoutRedirectUri: {1}, sessionId: {2}, Is Secure = {3}",
        idTokenHint, postLogoutRedirectUri, sessionId, sec.isSecure());
    Response.ResponseBuilder builder = Response.ok();

    if (!EndSessionParamsValidator.validateParams(idTokenHint, postLogoutRedirectUri)) {
      builder = Response.status(400);
      builder.entity(
          errorResponseFactory.getErrorAsJson(EndSessionErrorResponseType.INVALID_REQUEST));
    } else {
      AuthorizationGrant authorizationGrant =
          authorizationGrantList.getAuthorizationGrantByIdToken(idTokenHint);
      boolean isExternalAuthenticatorLogoutPresent = false;
      boolean externalLogoutResult = false;
      if (authorizationGrant != null) {
        removeSessionId(sessionId, httpRequest, httpResponse);

        isExternalAuthenticatorLogoutPresent = externalApplicationSessionService.isEnabled();
        if (isExternalAuthenticatorLogoutPresent) {
          externalLogoutResult =
              externalApplicationSessionService.executeExternalEndSessionMethods(
                  httpRequest, authorizationGrant);
          log.info(
              "End session result for '{0}': '{1}'",
              authorizationGrant.getUser().getUserId(), "logout", externalLogoutResult);
        }
      }
      boolean isGrantAndNoExternalLogout =
          authorizationGrant != null && !isExternalAuthenticatorLogoutPresent;
      boolean isGrantAndExternalLogoutSuccessful =
          authorizationGrant != null
              && isExternalAuthenticatorLogoutPresent
              && externalLogoutResult;
      if (isGrantAndNoExternalLogout || isGrantAndExternalLogoutSuccessful) {
        authorizationGrant.revokeAllTokens();

        // Validate redirectUri
        String redirectUri =
            redirectionUriService.validatePostLogoutRedirectUri(
                authorizationGrant.getClient().getClientId(), postLogoutRedirectUri);

        if (StringUtils.isNotBlank(redirectUri)) {
          RedirectUri redirectUriResponse = new RedirectUri(redirectUri);
          if (StringUtils.isNotBlank(state)) {
            redirectUriResponse.addResponseParameter(EndSessionResponseParam.STATE, state);
          }

          builder = RedirectUtil.getRedirectResponseBuilder(redirectUriResponse, httpRequest);
        } else {
          builder = Response.status(400);
          builder.entity(
              errorResponseFactory.getErrorAsJson(EndSessionErrorResponseType.INVALID_REQUEST));
        }
      } else {
        builder = Response.status(401);
        builder.entity(
            errorResponseFactory.getErrorAsJson(EndSessionErrorResponseType.INVALID_GRANT));
      }
    }
    return builder.build();
  }