yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
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
package com.qianwen.smartman.modules.auth.granter;
 
import io.jsonwebtoken.Claims;
import java.util.List;
import com.qianwen.smartman.common.utils.MessageUtils;
import com.qianwen.core.log.exception.ServiceException;
import com.qianwen.core.secure.utils.AuthUtil;
import com.qianwen.core.tool.utils.Func;
import com.qianwen.smartman.modules.auth.provider.ITokenGranter;
import com.qianwen.smartman.modules.auth.provider.TokenParameter;
import com.qianwen.smartman.modules.auth.utils.TokenUtil;
import com.qianwen.smartman.modules.system.entity.Tenant;
import com.qianwen.smartman.modules.system.entity.UserInfo;
import com.qianwen.smartman.modules.system.service.IRoleService;
import com.qianwen.smartman.modules.system.service.ITenantService;
import com.qianwen.smartman.modules.system.service.IUserService;
import org.springframework.stereotype.Component;
 
@Component
/* loaded from: blade-api.jar:BOOT-INF/classes/org/springblade/modules/auth/granter/RefreshTokenGranter.class */
public class RefreshTokenGranter implements ITokenGranter {
    public static final String GRANT_TYPE = "refresh_token";
    private final IUserService userService;
    private final IRoleService roleService;
    private final ITenantService tenantService;
 
    public RefreshTokenGranter(final IUserService userService, final IRoleService roleService, final ITenantService tenantService) {
        this.userService = userService;
        this.roleService = roleService;
        this.tenantService = tenantService;
    }
 
    @Override // org.springblade.modules.auth.provider.ITokenGranter
    public UserInfo grant(TokenParameter tokenParameter) {
        String tenantId = tokenParameter.getArgs().getStr("tenantId");
        String grantType = tokenParameter.getArgs().getStr("grantType");
        String refreshToken = tokenParameter.getArgs().getStr("refreshToken");
        UserInfo userInfo = null;
        if (!Func.isNoneBlank(new CharSequence[]{grantType, refreshToken}) || !grantType.equals(GRANT_TYPE)) {
            return null;
        }
        Claims claims = AuthUtil.parseJWT(refreshToken);
        if (Func.isEmpty(claims)) {
            return null;
        }
        String tokenType = Func.toStr(claims.get("token_type"));
        if (tokenType.equals(GRANT_TYPE)) {
            Tenant tenant = this.tenantService.getByTenantId(tenantId);
            if (TokenUtil.judgeTenant(tenant)) {
                throw new ServiceException(MessageUtils.message(TokenUtil.USER_HAS_NO_TENANT_PERMISSION, new Object[0]));
            }
            userInfo = this.userService.userInfo(Long.valueOf(Func.toLong(claims.get("user_id"))));
            userInfo.getUser().setDeptId(Func.toStr(claims.get("dept_id")));
            userInfo.getUser().setRoleId(Func.toStr(claims.get("role_id")));
            List<String> roleAliases = this.roleService.getRoleAliases(Func.toStr(claims.get("role_id")));
            userInfo.setRoles(roleAliases);
        }
        return userInfo;
    }
}