aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/service/security/HashParamAuthenticationFilter.java
blob: b56b98c8b49fcee7145cb20795c0a400b3127c5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * Copyright (C) 2008-2017, Juick
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.juick.service.security;

import com.juick.User;
import com.juick.service.security.entities.JuickUser;
import com.juick.service.UserService;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.RememberMeAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.AbstractRememberMeServices;
import org.springframework.util.Assert;
import org.springframework.web.filter.OncePerRequestFilter;
import org.springframework.web.util.WebUtils;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by aalexeev on 4/5/17.
 */
public class HashParamAuthenticationFilter extends OncePerRequestFilter {
    public static final String PARAM_NAME = "hash";

    private final UserService userService;
    private final RememberMeServices rememberMeServices;


    public HashParamAuthenticationFilter(
            final UserService userService,
            final RememberMeServices rememberMeServices) {
        Assert.notNull(userService, "userService should not be null");
        Assert.notNull(rememberMeServices, "rememberMeServices should not be null");

        this.userService = userService;
        this.rememberMeServices = rememberMeServices;
    }

    @Override
    protected void doFilterInternal(
            HttpServletRequest request,
            HttpServletResponse response,
            FilterChain filterChain) throws ServletException, IOException {

        String hash = getHashFromRequest(request);

        if (hash != null && authenticationIsRequired()) {
            User user = userService.getUserByHash(hash);

            if (!user.isAnonymous()) {
                User userWithPassword = userService.getFullyUserByName(user.getName());
                userWithPassword.setAuthHash(userService.getHashByUID(userWithPassword.getUid()));
                Authentication authentication = new RememberMeAuthenticationToken(
                        ((AbstractRememberMeServices)rememberMeServices).getKey(), new JuickUser(userWithPassword), JuickUser.USER_AUTHORITY);

                SecurityContextHolder.getContext().setAuthentication(authentication);

                rememberMeServices.loginSuccess(request, response, authentication);
            }
        }

        filterChain.doFilter(request, response);
    }

    private boolean authenticationIsRequired() {
        Authentication existingAuth = SecurityContextHolder.getContext().getAuthentication();

        return existingAuth == null ||
                !existingAuth.isAuthenticated() ||
                existingAuth instanceof AnonymousAuthenticationToken;
    }

    private String getHashFromRequest(HttpServletRequest request) {
        String paramHash = request.getParameter(PARAM_NAME);
        Cookie cookieHash = WebUtils.getCookie(request, PARAM_NAME);

        if (paramHash == null && cookieHash != null) {
            return cookieHash.getValue();
        }
        return paramHash;
    }
}