io.hawkcd.http.AuthController.java Source code

Java tutorial

Introduction

Here is the source code for io.hawkcd.http.AuthController.java

Source

/*
 * Copyright (C) 2016 R&D Solutions Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.hawkcd.http;

import com.google.gson.Gson;
import io.hawkcd.core.security.AuthorizationGrant;
import io.hawkcd.core.session.SessionFactory;
import io.hawkcd.http.security.PrincipalUser;
import io.hawkcd.http.security.Secured;
import io.hawkcd.model.ServiceResult;
import io.hawkcd.model.User;
import io.hawkcd.model.dto.LoginDto;
import io.hawkcd.model.dto.RegisterDto;
import io.hawkcd.model.enums.NotificationType;
import io.hawkcd.services.UserService;
import io.hawkcd.utilities.deserializers.TokenAdapter;
import io.swagger.annotations.Api;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//import com.sun.jersey.api.client.Client;
//import com.sun.jersey.api.client.ClientResponse;
//import com.sun.jersey.api.client.WebResource;

@Consumes("application/json")
@Produces("application/json")
@Path("auth")
@Api(value = "/auth", description = "Web Services to browse entities")
public class AuthController {
    private static final Logger LOGGER = Logger.getLogger(AuthController.class.getClass());
    private static final String GH_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
    //TODO: move this to the config
    private static final String GH_CLIENT_ID = "2d3dbbf586d2260cbd68";
    //TODO: move this to the config
    private static final String GH_CLIENT_SERCRET = "";

    /**
     * Holds the name of the business service PACKAGE the resource endpoints expose
     */
    public static final String SERVICE_PACKAGE_NAME = "io.hawkcd.services";

    /**
     * Holds the name of the business service the resource endpoints expose
     */
    public static final String SERVICE_CLASS_NAME = "UserService";

    private UserService userService;

    public AuthController() {
        this.userService = new UserService();
    }

    //    @POST
    //    @Consumes(MediaType.APPLICATION_JSON)
    //    @Produces(MediaType.APPLICATION_JSON)
    //    @Path("/github")
    //    public Response updateUserInfo(GithubAuthDto authDto) throws IOException {
    //
    //        //TODO: implement schema validation
    //        //extract authorization code
    //        String code = authDto.getCode();
    //        //request access token
    //        String accessToken = getGitHubAccessToken(code);
    //        //find out the gh user details
    //        GitHubService ghService = new GitHubService(accessToken);
    //        Map ghUserDetails = ghService.getMyself();
    //        //read user email
    //        String email = (String) ghUserDetails.get("email");
    //
    //        ArrayList<PrincipalUser> all = (ArrayList<PrincipalUser>) this.userService.getAll().getObject();
    //
    //        boolean isUserRegistered = false;
    //        //loop through users to check if the user already exists
    //
    //        PrincipalUser user = null;
    //        for(PrincipalUser usr:all){
    //            String uEmail = usr.getEmail();
    //            if ( uEmail.equals(email)){
    //                isUserRegistered = true;
    //                user = usr;
    //                break;
    //            }
    //        }
    //
    //        if (!isUserRegistered){
    //            //register a new user
    //            user =  new PrincipalUser();
    //            user.setEmail(email);
    //            user.setProvider("GitHub");
    //            userService.add(user);
    //        }
    //
    //
    //        return Response.status(Response.PipelineStatus.CREATED)
    //                .entity(user)
    //                .build();
    //    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    @Path("/login")
    public Response login(LoginDto login) throws IOException {
        String hashedPassword = DigestUtils.sha256Hex(login.getPassword());
        ServiceResult serviceResult = this.userService.getByEmailAndPassword(login.getEmail(), hashedPassword);
        if (serviceResult.getNotificationType() == NotificationType.ERROR) {
            return Response.status(Response.Status.BAD_REQUEST).entity(serviceResult).build();
        }

        User userFromDb = (User) serviceResult.getEntity();

        if (!userFromDb.isEnabled()) {
            serviceResult.setNotificationType(NotificationType.ERROR);
            serviceResult.setMessage("Cannot login");
            serviceResult.setEntity(null);
            return Response.status(Response.Status.FORBIDDEN).build();
        }

        String token = TokenAdapter.createJsonWebToken(userFromDb, 30L);

        Gson gson = new Gson();

        String jsonToken = gson.toJson(token);

        return Response.ok(jsonToken).build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces("application/json")
    @Path("/logout")
    @Secured
    public Response logout(@Context ContainerRequestContext requestContext) {
        PrincipalUser principalUser = (PrincipalUser) requestContext.getSecurityContext().getUserPrincipal();
        String userEmail = principalUser.getUser().getEmail();
        LOGGER.info("PrincipalUser: " + userEmail + " logged out");
        SessionFactory.getSessionManager().closeSessionByUserEmail(userEmail);

        return Response.ok().build();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/register")
    @Secured
    public Response register(RegisterDto newUser) {

        List<AuthorizationGrant> userPermissions = new ArrayList<>();

        User user = new User();
        user.setEmail(newUser.getEmail());
        user.setPassword(newUser.getPassword());

        //TODO: move this to update user, should not be here, left till adminUsers is ready
        if (newUser.getPermissions() != null) {
            userPermissions.addAll(newUser.getPermissions());
        }
        user.setPermissions(userPermissions);

        ServiceResult serviceResult = this.userService.addUserWithoutProvider(user);

        if (serviceResult.getNotificationType() == NotificationType.ERROR) {
            return Response.status(Response.Status.BAD_REQUEST).entity(serviceResult).build();
        } else {
            return Response.status(Response.Status.CREATED).entity(serviceResult).build();
        }
    }

    //    private String getGitHubAccessToken(String authCode) {
    //
    //        String ghResource = String.format("%s?client_id=%s&client_secret=%s&code=%s",GH_ACCESS_TOKEN_URL,GH_CLIENT_ID,GH_CLIENT_SERCRET,authCode);
    //        Client client = Client.create();
    //        WebResource resource = client.resource(ghResource);
    //
    //        ClientResponse response = resource.get(ClientResponse.class);
    //        if (response.getStatus() !=200 ){
    //            throw new RuntimeException("Failed : HTTP error code : "
    //                    + response.getStatus());
    //        }
    //
    //        String stringToMatch = response.getEntity(String.class);
    //
    //        //run through regext to extract the access_token
    //        Pattern p = Pattern.compile("access_token=.*?&");
    //        Matcher m = p.matcher(stringToMatch);
    //        String at = null;
    //        if (m.find()){
    //            at = m.group(0);
    //        }
    //        if (at!=null){
    //            return at.replace("access_token=","").replace("&","");
    //        }else
    //        {
    //            return null;
    //        }
    //    }

}