Search in sources :

Example 6 with User

use of com.authlete.common.types.User in project java-oauth-server by authlete.

the class AuthorizationDecisionEndpoint method post.

/**
 * Process a request from the form in the authorization page.
 *
 * <p>
 * NOTE:
 * A better implementation would re-display the authorization page
 * when the pair of login ID and password is wrong, but this
 * implementation does not do it for brevity. A much better
 * implementation would check the login credentials by Ajax.
 * </p>
 *
 * @param request
 *         A request from the form in the authorization page.
 *
 * @param parameters
 *         Request parameters.
 *
 * @return
 *         A response to the user agent. Basically, the response
 *         will trigger redirection to the client's redirect
 *         endpoint.
 */
@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response post(@Context HttpServletRequest request, MultivaluedMap<String, String> parameters) {
    // Get the existing session.
    HttpSession session = getSession(request);
    // Retrieve some variables from the session. See the implementation
    // of AuthorizationRequestHandlerSpiImpl.getAuthorizationPage().
    Params params = (Params) takeAttribute(session, "params");
    String[] acrs = (String[]) takeAttribute(session, "acrs");
    Client client = (Client) takeAttribute(session, "client");
    User user = getUser(session, parameters);
    Date authTime = (Date) session.getAttribute("authTime");
    // Implementation of AuthorizationDecisionHandlerSpi.
    AuthorizationDecisionHandlerSpi spi = new AuthorizationDecisionHandlerSpiImpl(parameters, user, authTime, params.getIdTokenClaims(), acrs, client);
    // Handle the end-user's decision.
    return handle(AuthleteApiFactory.getDefaultApi(), spi, params);
}
Also used : User(com.authlete.common.types.User) HttpSession(javax.servlet.http.HttpSession) Params(com.authlete.jaxrs.AuthorizationDecisionHandler.Params) AuthorizationDecisionHandlerSpi(com.authlete.jaxrs.spi.AuthorizationDecisionHandlerSpi) Client(com.authlete.common.dto.Client) Date(java.util.Date) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 7 with User

use of com.authlete.common.types.User in project java-oauth-server by authlete.

the class AuthorizationDecisionEndpoint method getUser.

/**
 * Look up an end-user.
 */
private static User getUser(HttpSession session, MultivaluedMap<String, String> parameters) {
    // Look up the user in the session to see if they're already logged in.
    User sessionUser = (User) session.getAttribute("user");
    if (sessionUser != null) {
        return sessionUser;
    }
    // Look up an end-user who has the login credentials.
    User loginUser = UserDao.getByCredentials(parameters.getFirst("loginId"), parameters.getFirst("password"));
    if (loginUser != null) {
        session.setAttribute("user", loginUser);
        session.setAttribute("authTime", new Date());
    }
    return loginUser;
}
Also used : User(com.authlete.common.types.User) Date(java.util.Date)

Example 8 with User

use of com.authlete.common.types.User in project java-oauth-server by authlete.

the class FederationEndpoint method callback.

@GET
@Path("callback/{federationId}")
public Response callback(@Context HttpServletRequest req, @PathParam("federationId") String federationId) {
    // Authentication response from the OpenID Provider.
    URI authenticationResponse = getFullUri(req);
    // Get the Federation instance that corresponds to the federation ID.
    Federation federation = getFederation(federationId);
    // Data used to render the authorization page.
    AuthzPageModel model = getAuthzPageModel(req);
    // "state" and "code_verifier" which were generated in initiation().
    String state = takeFromSession(req, KEY_STATE);
    String verifier = takeFromSession(req, KEY_VERIFIER);
    // Ensure that 'state' is available.
    ensureState(state);
    // Communicate with the OpenID Provider to get information about the user.
    UserInfo userInfo = getUserInfo(federation, authenticationResponse, state, verifier, model);
    // Register the user into this server (or overwrite the existing info).
    User user = registerUser(federation, userInfo);
    // Make the user login.
    makeUserLogin(req, user);
    // Go back to the authorization page.
    return authorizationPage(model, user, null);
}
Also used : User(com.authlete.common.types.User) Federation(com.authlete.jaxrs.server.federation.Federation) UserInfo(com.nimbusds.openid.connect.sdk.claims.UserInfo) URI(java.net.URI) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 9 with User

use of com.authlete.common.types.User in project java-oauth-server by authlete.

the class DeviceVerificationEndpoint method authenticateUser.

private void authenticateUser(HttpSession session, MultivaluedMap<String, String> parameters) {
    // Look up the user in the session to see if they're already logged in.
    User sessionUser = (User) session.getAttribute("user");
    if (sessionUser != null) {
        // OK. The user has been already authenticated.
        return;
    }
    // The user has not been authenticated yet. Then, check the user credentials
    // in the submitted parameters
    // Look up an end-user who has the login credentials.
    User loginUser = UserDao.getByCredentials(parameters.getFirst("loginId"), parameters.getFirst("password"));
    if (loginUser != null) {
        // OK. The user having the credentials was found.
        // Set the login information about the user in the session.
        session.setAttribute("user", loginUser);
        session.setAttribute("authTime", new Date());
        return;
    }
    // Error. The user authentication has failed.
    // Urge the user to input valid login credentials again.
    // The model for rendering the verification page.
    DeviceVerificationPageModel model = new DeviceVerificationPageModel().setLoginId(parameters.getFirst("loginId")).setUserCode(parameters.getFirst("userCode")).setNotification("User authentication failed.");
    // Throw a "401 Unauthorized" exception and show the verification page.
    throw unauthorizedException(new Viewable(TEMPLATE, model), CHALLENGE);
}
Also used : User(com.authlete.common.types.User) DeviceVerificationPageModel(com.authlete.jaxrs.DeviceVerificationPageModel) Viewable(org.glassfish.jersey.server.mvc.Viewable) Date(java.util.Date)

Example 10 with User

use of com.authlete.common.types.User in project java-oauth-server by authlete.

the class DeviceVerificationEndpoint method get.

/**
 * The verification endpoint for {@code GET} method. This method returns a
 * verification page where the end-user is asked to input her login credentials
 * (if not authenticated) and a user code.
 */
@GET
public Response get(@Context HttpServletRequest request, @Context UriInfo uriInfo) {
    // Get user information from the existing session if present.
    User user = getUserFromSessionIfPresent(request);
    // Get the user code from the query parameters if present.
    String userCode = uriInfo.getQueryParameters().getFirst("user_code");
    // The model for rendering the verification page.
    DeviceVerificationPageModel model = new DeviceVerificationPageModel().setUser(user).setUserCode(userCode);
    // Create a response of "200 OK" having the verification page.
    return ok(new Viewable(TEMPLATE, model));
}
Also used : User(com.authlete.common.types.User) DeviceVerificationPageModel(com.authlete.jaxrs.DeviceVerificationPageModel) Viewable(org.glassfish.jersey.server.mvc.Viewable) GET(javax.ws.rs.GET)

Aggregations

User (com.authlete.common.types.User)13 Date (java.util.Date)6 HttpSession (javax.servlet.http.HttpSession)5 Viewable (org.glassfish.jersey.server.mvc.Viewable)4 DeviceVerificationPageModel (com.authlete.jaxrs.DeviceVerificationPageModel)3 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 POST (javax.ws.rs.POST)2 Result (com.authlete.common.dto.BackchannelAuthenticationCompleteRequest.Result)1 Client (com.authlete.common.dto.Client)1 Params (com.authlete.jaxrs.AuthorizationDecisionHandler.Params)1 BackchannelAuthenticationCompleteRequestHandler (com.authlete.jaxrs.BackchannelAuthenticationCompleteRequestHandler)1 Federation (com.authlete.jaxrs.server.federation.Federation)1 AuthorizationDecisionHandlerSpi (com.authlete.jaxrs.spi.AuthorizationDecisionHandlerSpi)1 UserInfo (com.nimbusds.openid.connect.sdk.claims.UserInfo)1 URI (java.net.URI)1 Path (javax.ws.rs.Path)1