AbstractRemeberMeManager.java 4.1 KB
Newer Older
M
MaxKey 已提交
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
/*
 * Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
 * 
 * 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 org.maxkey.authn.support.rememberme;

import java.text.ParseException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.maxkey.authn.SignPrincipal;
M
MaxKey 已提交
27
import org.maxkey.authn.jwt.AuthTokenService;
M
MaxKey 已提交
28 29 30 31 32 33 34 35 36 37 38
import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.crypto.jwt.HMAC512Service;
import org.maxkey.entity.UserInfo;
import org.maxkey.util.DateUtils;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;

import com.nimbusds.jwt.JWTClaimsSet;

M
MaxKey 已提交
39 40
public abstract class AbstractRemeberMeManager {
    private static final Logger _logger = LoggerFactory.getLogger(AbstractRemeberMeManager.class);
M
MaxKey 已提交
41 42 43 44 45

    protected Integer validity = 7;

    protected ApplicationConfig applicationConfig;
    
M
MaxKey 已提交
46
    AuthTokenService authTokenService;
M
MaxKey 已提交
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

    // follow function is for persist
    public abstract void save(RemeberMe remeberMe);

    public abstract void update(RemeberMe remeberMe);

    public abstract RemeberMe read(RemeberMe remeberMe);

    public abstract void remove(String username);
    // end persist

    public String createRemeberMe(Authentication  authentication, 
    					HttpServletRequest request, HttpServletResponse response) {
        if (applicationConfig.getLoginConfig().isRemeberMe()) {
        	SignPrincipal principal = ((SignPrincipal)authentication.getPrincipal());
    		UserInfo userInfo = principal.getUserInfo();
            _logger.debug("Remeber Me ...");
            RemeberMe remeberMe = new RemeberMe();
            remeberMe.setId(WebContext.genId());
            remeberMe.setUserId(userInfo.getId());
            remeberMe.setUsername(userInfo.getUsername());
            remeberMe.setLastLoginTime(DateUtils.getCurrentDate());
            remeberMe.setExpirationTime(DateTime.now().plusDays(validity).toDate());
            save(remeberMe);
            _logger.debug("Remeber Me " + remeberMe);
            return genRemeberMe(remeberMe);
        }
        return null;
    }

    public String updateRemeberMe(RemeberMe remeberMe) {
        remeberMe.setLastLoginTime(new Date());
        remeberMe.setExpirationTime(DateTime.now().plusDays(validity).toDate());
        update(remeberMe);
        _logger.debug("update Remeber Me " + remeberMe);
        
        return genRemeberMe(remeberMe);
    }

    public boolean removeRemeberMe(HttpServletResponse response,UserInfo currentUser) {
        remove(currentUser.getUsername());

        return true;
    }
    
M
MaxKey 已提交
92
    public RemeberMe resolve(String rememberMeJwt) throws ParseException {
M
MaxKey 已提交
93
    	JWTClaimsSet claims = authTokenService.resolve(rememberMeJwt);
M
MaxKey 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    	RemeberMe remeberMe = new RemeberMe();
		remeberMe.setId(claims.getJWTID());
		remeberMe.setUsername(claims.getSubject());
		return read(remeberMe);
    }
    
    public String genRemeberMe(RemeberMe remeberMe ) {
		_logger.debug("expiration Time : {}" , remeberMe.getExpirationTime());
		
		 JWTClaimsSet remeberMeJwtClaims =new  JWTClaimsSet.Builder()
				.issuer("")
				.subject(remeberMe.getUsername())
				.jwtID(remeberMe.getId())
				.issueTime(remeberMe.getLastLoginTime())
				.expirationTime(remeberMe.getExpirationTime())
				.claim("kid", HMAC512Service.MXK_AUTH_JWK)
				.build();
		
M
MaxKey 已提交
112
		return authTokenService.signedJWT(remeberMeJwtClaims);
M
MaxKey 已提交
113 114 115 116 117 118 119
	}

	public Integer getValidity() {
		return validity;
	}

	public void setValidity(Integer validity) {
M
MaxKey 已提交
120 121 122
		if(validity != 0 ) {
			this.validity = validity;
		}
M
MaxKey 已提交
123 124 125 126
	}
    

}