Package com.walters.sms.rest

Source Code of com.walters.sms.rest.AuthenticationService

package com.walters.sms.rest;

import javax.annotation.security.PermitAll;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import com.walters.common.args.CheckArg;
import com.walters.common.utils.StringUtilities;
import com.walters.sms.IServiceLocator;
import com.walters.sms.IUserAuthenticationService;
import com.walters.sms.ServiceLocator;
import com.walters.sms.domain.User;
import com.walters.sms.rest.domain.UserToken;

@Path("/auth")
public class AuthenticationService {

  @PermitAll
  @GET
  @Path("/login/{username}:{password}")
  @Produces("application/json")
  public UserToken login(@PathParam("username") String username, @PathParam("password") String password) {

    final User authenticatedUser = authenticate(username, password);

    return createUserToken(authenticatedUser);
  }

  protected User authenticate(final String username, final String password) {
    User user = null;
   
    boolean isUserAuthenticated = getUserAuthenticationService().authenticateUser(username, password);

    if (isUserAuthenticated) {
      user = CheckArg.getNotNull(getUserAuthenticationService().getCurrentUser(), "currentUser");
    }
   
    return user;
  }

  protected boolean isUserAuthenticated() {
    return getUserAuthenticationService().isUserAuthenticated();
  }

  private IUserAuthenticationService getUserAuthenticationService() {
    return getServiceLocator().getService(IUserAuthenticationService.class);
  }

  private IServiceLocator getServiceLocator() {
    return ServiceLocator.getInstance();
  }

  private UserToken createUserToken(final User user) {
    UserToken userToken = null;

    if(CheckArg.isNotNull(user)){
      final long userId = user.getId();
      final String username = user.getUsername();
      final String password = user.getPassword();
      final String sessionToken = StringUtilities.encodeToBase24String(username + ":" + password);

      userToken = new UserToken(userId, sessionToken);
    }

    return userToken;
  }
}
TOP

Related Classes of com.walters.sms.rest.AuthenticationService

TOP
Copyright © 2018 www.massapi.com. 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.