提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.convert.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.core.date.LocalDateTimeUtil; |
|
4 |
import com.iailab.framework.common.core.KeyValue; |
|
5 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
6 |
import com.iailab.framework.common.util.collection.CollectionUtils; |
|
7 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
8 |
import com.iailab.framework.security.core.util.SecurityFrameworkUtils; |
|
9 |
import com.iailab.module.system.controller.admin.oauth2.vo.open.OAuth2OpenAccessTokenRespVO; |
|
10 |
import com.iailab.module.system.controller.admin.oauth2.vo.open.OAuth2OpenAuthorizeInfoRespVO; |
|
11 |
import com.iailab.module.system.controller.admin.oauth2.vo.open.OAuth2OpenCheckTokenRespVO; |
|
12 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
|
13 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ApproveDO; |
|
14 |
import com.iailab.module.system.dal.dataobject.oauth2.OAuth2ClientDO; |
|
15 |
import com.iailab.module.system.util.oauth2.OAuth2Utils; |
|
16 |
import org.mapstruct.Mapper; |
|
17 |
import org.mapstruct.factory.Mappers; |
|
18 |
|
|
19 |
import java.util.ArrayList; |
|
20 |
import java.util.List; |
|
21 |
import java.util.Map; |
|
22 |
|
|
23 |
@Mapper |
|
24 |
public interface OAuth2OpenConvert { |
|
25 |
|
|
26 |
OAuth2OpenConvert INSTANCE = Mappers.getMapper(OAuth2OpenConvert.class); |
|
27 |
|
|
28 |
default OAuth2OpenAccessTokenRespVO convert(OAuth2AccessTokenDO bean) { |
|
29 |
OAuth2OpenAccessTokenRespVO respVO = BeanUtils.toBean(bean, OAuth2OpenAccessTokenRespVO.class); |
|
30 |
respVO.setTokenType(SecurityFrameworkUtils.AUTHORIZATION_BEARER.toLowerCase()); |
|
31 |
respVO.setExpiresIn(OAuth2Utils.getExpiresIn(bean.getExpiresTime())); |
|
32 |
respVO.setScope(OAuth2Utils.buildScopeStr(bean.getScopes())); |
|
33 |
return respVO; |
|
34 |
} |
|
35 |
|
|
36 |
default OAuth2OpenCheckTokenRespVO convert2(OAuth2AccessTokenDO bean) { |
|
37 |
OAuth2OpenCheckTokenRespVO respVO = BeanUtils.toBean(bean, OAuth2OpenCheckTokenRespVO.class); |
|
38 |
respVO.setExp(LocalDateTimeUtil.toEpochMilli(bean.getExpiresTime()) / 1000L); |
|
39 |
respVO.setUserType(UserTypeEnum.ADMIN.getValue()); |
|
40 |
return respVO; |
|
41 |
} |
|
42 |
|
|
43 |
default OAuth2OpenAuthorizeInfoRespVO convert(OAuth2ClientDO client, List<OAuth2ApproveDO> approves) { |
|
44 |
// 构建 scopes |
|
45 |
List<KeyValue<String, Boolean>> scopes = new ArrayList<>(client.getScopes().size()); |
|
46 |
Map<String, OAuth2ApproveDO> approveMap = CollectionUtils.convertMap(approves, OAuth2ApproveDO::getScope); |
|
47 |
client.getScopes().forEach(scope -> { |
|
48 |
OAuth2ApproveDO approve = approveMap.get(scope); |
|
49 |
scopes.add(new KeyValue<>(scope, approve != null ? approve.getApproved() : false)); |
|
50 |
}); |
|
51 |
// 拼接返回 |
|
52 |
return new OAuth2OpenAuthorizeInfoRespVO( |
|
53 |
new OAuth2OpenAuthorizeInfoRespVO.Client(client.getName(), client.getLogo()), scopes); |
|
54 |
} |
|
55 |
|
|
56 |
} |