|
|
|
@ -52,34 +52,6 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class UserLoginService {
|
|
|
|
|
private static final String WE_COM = "WE_COM";
|
|
|
|
|
|
|
|
|
|
private static final String DING = "DING_TALK";
|
|
|
|
|
|
|
|
|
|
private static final String LARK = "LARK";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_SUITE = "LARK_SUITE";
|
|
|
|
|
|
|
|
|
|
private static final String WE_COM_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s";
|
|
|
|
|
|
|
|
|
|
private static final String WE_COM_USERID_URL = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=%s&code=%s";
|
|
|
|
|
private static final String WE_COM_USERINFO_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=%s&userid=%s";
|
|
|
|
|
private static final String DING_USER_INFO = "https://api.dingtalk.com/v1.0/contact/users/me";
|
|
|
|
|
private static final String DING_USER_TOKEN_URL = "https://api.dingtalk.com/v1.0/oauth2/userAccessToken";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_USER_TOKEN_URL = "https://open.feishu.cn/open-apis/authen/v1/oidc/access_token";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_SUITE_USER_TOKEN_URL = "https://open.larksuite.com/open-apis/authen/v1/oidc/access_token";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String LARK_APP_TOKEN_URL = "https://open.feishu.cn/open-apis/auth/v3/app_access_token/internal";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_SUITE_APP_TOKEN_URL = "https://open.larksuite.com/open-apis/auth/v3/app_access_token/internal";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_SUITE_USER_INFO_URL = "https://open.larksuite.com/open-apis/authen/v1/user_info";
|
|
|
|
|
|
|
|
|
|
private static final String LARK_USER_INFO_URL = "https://open.feishu.cn/open-apis/authen/v1/user_info";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private UserMapper userMapper;
|
|
|
|
@ -94,17 +66,6 @@ public class UserLoginService {
|
|
|
|
|
@Resource
|
|
|
|
|
private BaseProjectMapper baseProjectMapper;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private PlatformSourceMapper platformSourceMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
private QrCodeClient qrCodeClient;
|
|
|
|
|
@Resource
|
|
|
|
|
private WorkspaceMapper workspaceMapper;
|
|
|
|
|
@Resource
|
|
|
|
|
SqlSessionFactory sqlSessionFactory;
|
|
|
|
|
@Resource
|
|
|
|
|
private OperatingLogService operatingLogService;
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> login(LoginRequest request, WebSession session, Locale locale) {
|
|
|
|
|
UserDTO userDTO;
|
|
|
|
|
if (locale != null) {
|
|
|
|
@ -554,355 +515,5 @@ public class UserLoginService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> exchangeWeComToken(String code, WebSession session, Locale locale) {
|
|
|
|
|
WeComInfoDTO vo = getWeComInfo(WE_COM);
|
|
|
|
|
String accessToken = generateWeComToken(vo);
|
|
|
|
|
String url = String.format(WE_COM_USERID_URL, accessToken, code);
|
|
|
|
|
String body = qrCodeClient.get(url);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(bodyMap.get("errcode")) && Integer.parseInt(bodyMap.get("errcode").toString()) != 0) {
|
|
|
|
|
MSException.throwException("获取USERID失败:" + bodyMap.get("errmsg"));
|
|
|
|
|
}
|
|
|
|
|
if (!bodyMap.containsKey("userid") && bodyMap.containsKey("openid")) {
|
|
|
|
|
MSException.throwException("当前用户非企业成员,禁止登录操作");
|
|
|
|
|
}
|
|
|
|
|
String userId = bodyMap.get("userid").toString();
|
|
|
|
|
String userInfoUrl = String.format(WE_COM_USERINFO_URL, accessToken, userId);
|
|
|
|
|
String userJson = qrCodeClient.get(userInfoUrl);
|
|
|
|
|
Map userMap = JSON.parseMap(userJson);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(userMap.get("errcode")) && Integer.parseInt(userMap.get("errcode").toString()) != 0) {
|
|
|
|
|
MSException.throwException("获取用户详情失败:" + userMap.get("errmsg"));
|
|
|
|
|
}
|
|
|
|
|
String mobile = ObjectUtils.isNotEmpty(userMap.get("mobile")) ? userMap.get("mobile").toString() : "";
|
|
|
|
|
String name = userMap.get("name").toString();
|
|
|
|
|
String email = ObjectUtils.isNotEmpty(userMap.get("email")) ? userMap.get("email").toString() : ObjectUtils.isNotEmpty(userMap.get("biz_mail")) ? userMap.get("biz_mail").toString() : "";
|
|
|
|
|
UserRequest userCreateInfo = getUserRequest(userId, email, name, mobile);
|
|
|
|
|
return login(userCreateInfo, UserSource.QR_CODE.name(), session, locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> login(UserRequest userCreateInfo, String source, WebSession session, Locale locale) {
|
|
|
|
|
String userId = userCreateInfo.getId();
|
|
|
|
|
String email = userCreateInfo.getEmail();
|
|
|
|
|
String name = userCreateInfo.getName();
|
|
|
|
|
String phone = userCreateInfo.getPhone();
|
|
|
|
|
//区分有无email
|
|
|
|
|
UserDTO userDTOByEmail;
|
|
|
|
|
boolean changeMail = false;
|
|
|
|
|
try {
|
|
|
|
|
UserDTO userDTO = getUserDTO(userId);
|
|
|
|
|
if (StringUtils.isNotBlank(email)) {
|
|
|
|
|
userDTOByEmail = getUserDTOByEmail(email);
|
|
|
|
|
if (userDTO != null && userDTOByEmail == null) {
|
|
|
|
|
userDTOByEmail = new UserDTO();
|
|
|
|
|
BeanUtils.copyProperties(userDTO, userDTOByEmail);
|
|
|
|
|
userDTOByEmail.setEmail(email);
|
|
|
|
|
changeMail = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
email = userId + "@metersphere.io";
|
|
|
|
|
userDTOByEmail = getUserDTOByEmail(email);
|
|
|
|
|
if (userDTOByEmail == null) {
|
|
|
|
|
userDTOByEmail = getUserDTO(userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ExcessiveAttemptsException e) {
|
|
|
|
|
throw new ExcessiveAttemptsException(Translator.get("excessive_attempts"));
|
|
|
|
|
} catch (LockedAccountException e) {
|
|
|
|
|
throw new LockedAccountException(Translator.get("user_locked"));
|
|
|
|
|
} catch (DisabledAccountException e) {
|
|
|
|
|
throw new DisabledAccountException(Translator.get("user_has_been_disabled"));
|
|
|
|
|
} catch (ExpiredCredentialsException e) {
|
|
|
|
|
throw new ExpiredCredentialsException(Translator.get("user_expires"));
|
|
|
|
|
} catch (AuthenticationException e) {
|
|
|
|
|
throw new AuthenticationException(e.getMessage());
|
|
|
|
|
} catch (UnauthorizedException e) {
|
|
|
|
|
throw new UnauthorizedException(Translator.get("not_authorized") + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
if (userDTOByEmail == null) {
|
|
|
|
|
LogUtil.info("creatUser");
|
|
|
|
|
LogUtil.info(email);
|
|
|
|
|
LogUtil.info(name);
|
|
|
|
|
LogUtil.info(userId);
|
|
|
|
|
userCreateInfo.setEmail(email);
|
|
|
|
|
creatUser(userCreateInfo, source);
|
|
|
|
|
} else {
|
|
|
|
|
userId = userDTOByEmail.getId();
|
|
|
|
|
if (StringUtils.equals(userDTOByEmail.getStatus(), UserStatus.DISABLED)) {
|
|
|
|
|
MSException.throwException("user is disabled!");
|
|
|
|
|
}
|
|
|
|
|
LogUtil.info(userId);
|
|
|
|
|
LogUtil.info(userDTOByEmail.getStatus());
|
|
|
|
|
if (changeMail) {
|
|
|
|
|
updateUser(email, userId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
LogUtil.info("login user");
|
|
|
|
|
LogUtil.info(email);
|
|
|
|
|
LogUtil.info(name);
|
|
|
|
|
LogUtil.info(userId);
|
|
|
|
|
LoginRequest request = new LoginRequest();
|
|
|
|
|
try {
|
|
|
|
|
request.setAuthenticate(source);
|
|
|
|
|
request.setUsername(userId);
|
|
|
|
|
request.setPassword(email);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
LogUtil.error("login error: ", e);
|
|
|
|
|
MSException.throwException("login error: " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return login(request, session, locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateUser(String email, String userId) {
|
|
|
|
|
User updateUser = new User();
|
|
|
|
|
updateUser.setId(userId);
|
|
|
|
|
updateUser.setEmail(email);
|
|
|
|
|
updateUser.setUpdateTime(System.currentTimeMillis());
|
|
|
|
|
updateUser.setCreateUser(null);
|
|
|
|
|
updateUser.setUpdateTime(null);
|
|
|
|
|
userMapper.updateByPrimaryKeySelective(updateUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public WeComInfoDTO getWeComInfo(String key) {
|
|
|
|
|
PlatformSource platformSource = platformSourceMapper.selectByPrimaryKey(key);
|
|
|
|
|
WeComInfoDTO weComInfoDTO = new WeComInfoDTO();
|
|
|
|
|
WeComCreator weComCreator = JSON.parseObject(platformSource.getConfig(), WeComCreator.class);
|
|
|
|
|
BeanUtils.copyProperties(weComCreator, weComInfoDTO);
|
|
|
|
|
weComInfoDTO.setEnable(platformSource.getEnable());
|
|
|
|
|
weComInfoDTO.setValid(platformSource.getValid());
|
|
|
|
|
return weComInfoDTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DingTalkInfoDTO getDingInfo(String key) {
|
|
|
|
|
PlatformSource platformSource = platformSourceMapper.selectByPrimaryKey(key);
|
|
|
|
|
DingTalkInfoDTO dingTalkInfoDTO = new DingTalkInfoDTO();
|
|
|
|
|
DingTalkCreator dingTalkCreator = JSON.parseObject(platformSource.getConfig(), DingTalkCreator.class);
|
|
|
|
|
BeanUtils.copyProperties(dingTalkCreator, dingTalkInfoDTO);
|
|
|
|
|
dingTalkInfoDTO.setEnable(platformSource.getEnable());
|
|
|
|
|
dingTalkInfoDTO.setValid(platformSource.getValid());
|
|
|
|
|
return dingTalkInfoDTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String generateWeComToken(WeComInfoDTO vo) {
|
|
|
|
|
if (ObjectUtils.isEmpty(vo)) {
|
|
|
|
|
vo = getWeComInfo(WE_COM);
|
|
|
|
|
}
|
|
|
|
|
String corpid = vo.getCorpId();
|
|
|
|
|
String appSecret = vo.getAppSecret();
|
|
|
|
|
String url = String.format(WE_COM_TOKEN_URL, corpid, appSecret);
|
|
|
|
|
String body = qrCodeClient.get(url);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(bodyMap.get("errcode")) && Integer.parseInt(bodyMap.get("errcode").toString()) != 0) {
|
|
|
|
|
throw new RuntimeException("获取accessToken失败:" + bodyMap.get("errmsg"));
|
|
|
|
|
}
|
|
|
|
|
return bodyMap.get("access_token").toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void creatUser(UserRequest userCreateInfo, String source) {
|
|
|
|
|
User user = new User();
|
|
|
|
|
BeanUtils.copyProperties(userCreateInfo, user);
|
|
|
|
|
user.setCreateTime(System.currentTimeMillis());
|
|
|
|
|
user.setCreateUser("admin");
|
|
|
|
|
user.setUpdateTime(System.currentTimeMillis());
|
|
|
|
|
// 默认1:启用状态
|
|
|
|
|
user.setStatus(UserStatus.NORMAL);
|
|
|
|
|
user.setSource(source);
|
|
|
|
|
// 密码使用 MD5
|
|
|
|
|
user.setEmail(user.getEmail());
|
|
|
|
|
user.setPassword(CodingUtil.md5(user.getEmail()));
|
|
|
|
|
userMapper.insertSelective(user);
|
|
|
|
|
//获取默认空间
|
|
|
|
|
Workspace workspace = getWorkspace();
|
|
|
|
|
//获取默认项目
|
|
|
|
|
Project project = getProject(workspace);
|
|
|
|
|
//添加用户组
|
|
|
|
|
addRole(workspace, project, user);
|
|
|
|
|
//添加日志
|
|
|
|
|
addLog(user, project);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String s = CodingUtil.md5("MiPiSU5wRvpn9JcmUsYJubXAiEiE@metersphere.io");
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addLog(User user, Project project) {
|
|
|
|
|
OperatingLogWithBLOBs log = new OperatingLogWithBLOBs();
|
|
|
|
|
log.setOperTitle(user.getName());
|
|
|
|
|
log.setOperContent(user.getName());
|
|
|
|
|
log.setProjectId(project.getId());
|
|
|
|
|
log.setOperPath("/sso/callback/we_com");
|
|
|
|
|
log.setId(UUID.randomUUID().toString());
|
|
|
|
|
log.setOperType(OperLogConstants.CREATE.name());
|
|
|
|
|
log.setOperModule(OperLogModule.SYSTEM_PARAMETER_SETTING);
|
|
|
|
|
List<DetailColumn> columns = new LinkedList<>();
|
|
|
|
|
OperatingLogDetails details = new OperatingLogDetails(user.getId(), project.getId(), user.getName(),
|
|
|
|
|
user.getCreateUser(), columns);
|
|
|
|
|
log.setOperContent(JSON.toJSONString(details));
|
|
|
|
|
log.setOperTime(System.currentTimeMillis());
|
|
|
|
|
log.setCreateUser(user.getCreateUser());
|
|
|
|
|
log.setOperUser(user.getCreateUser());
|
|
|
|
|
log.setSourceId(user.getId());
|
|
|
|
|
operatingLogService.create(log, log.getSourceId());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void addRole(Workspace workspace, Project project, User user) {
|
|
|
|
|
Map<String, String> userRoleMap = new HashMap<>();
|
|
|
|
|
userRoleMap.put(UserGroupConstants.WS_MEMBER, workspace.getId());
|
|
|
|
|
userRoleMap.put(UserGroupConstants.PROJECT_MEMBER, project.getId());
|
|
|
|
|
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
|
|
|
|
|
UserGroupMapper batchSaveMapper = sqlSession.getMapper(UserGroupMapper.class);
|
|
|
|
|
userRoleMap.forEach((k, v) -> {
|
|
|
|
|
UserGroup userGroup = new UserGroup();
|
|
|
|
|
userGroup.setId(UUID.randomUUID().toString());
|
|
|
|
|
userGroup.setUserId(user.getId());
|
|
|
|
|
userGroup.setGroupId(k);
|
|
|
|
|
userGroup.setSourceId(v);
|
|
|
|
|
userGroup.setCreateTime(System.currentTimeMillis());
|
|
|
|
|
userGroup.setUpdateTime(System.currentTimeMillis());
|
|
|
|
|
batchSaveMapper.insertSelective(userGroup);
|
|
|
|
|
});
|
|
|
|
|
sqlSession.flushStatements();
|
|
|
|
|
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Project getProject(Workspace Workspace) {
|
|
|
|
|
ProjectExample projectExample = new ProjectExample();
|
|
|
|
|
projectExample.setOrderByClause("create_time ASC");
|
|
|
|
|
projectExample.createCriteria().andWorkspaceIdEqualTo(Workspace.getId()).andVersionEnableEqualTo(true);
|
|
|
|
|
List<Project> projects = projectMapper.selectByExample(projectExample);
|
|
|
|
|
return projects.get(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Workspace getWorkspace() {
|
|
|
|
|
WorkspaceExample WorkspaceExample = new WorkspaceExample();
|
|
|
|
|
WorkspaceExample.setOrderByClause("create_time ASC");
|
|
|
|
|
List<Workspace> Workspaces = workspaceMapper.selectByExample(WorkspaceExample);
|
|
|
|
|
return Workspaces.get(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> exchangeDingTalkToken(String authCode, WebSession session, Locale locale) {
|
|
|
|
|
DingTalkInfoDTO vo = getDingInfo(DING);
|
|
|
|
|
String dingToken = generateDingUserToken(vo, authCode);
|
|
|
|
|
String body = qrCodeClient.exchange(DING_USER_INFO, dingToken, "x-acs-dingtalk-access-token", MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
String userId = bodyMap.get("unionId").toString();
|
|
|
|
|
String name = bodyMap.get("nick").toString();
|
|
|
|
|
String email = ObjectUtils.isNotEmpty(bodyMap.get("email")) ? bodyMap.get("email").toString() : "";
|
|
|
|
|
String mobile = ObjectUtils.isNotEmpty(bodyMap.get("mobile")) ? bodyMap.get("mobile").toString() : "";
|
|
|
|
|
UserRequest userCreateInfo = getUserRequest(userId, email, name, mobile);
|
|
|
|
|
return login(userCreateInfo, UserSource.QR_CODE.name(), session, locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String generateDingUserToken(DingTalkInfoDTO vo, String code) {
|
|
|
|
|
if (ObjectUtils.isEmpty(vo)) {
|
|
|
|
|
vo = getDingInfo(DING);
|
|
|
|
|
}
|
|
|
|
|
String appKey = vo.getAppKey();
|
|
|
|
|
String appSecret = vo.getAppSecret();
|
|
|
|
|
DingTalkTokenParamDTO dingTalkTokenParamDTO = new DingTalkTokenParamDTO();
|
|
|
|
|
dingTalkTokenParamDTO.setClientId(appKey);
|
|
|
|
|
dingTalkTokenParamDTO.setClientSecret(appSecret);
|
|
|
|
|
dingTalkTokenParamDTO.setCode(code);
|
|
|
|
|
dingTalkTokenParamDTO.setGrantType("authorization_code");
|
|
|
|
|
String body = qrCodeClient.postExchange(DING_USER_TOKEN_URL, null, null, dingTalkTokenParamDTO, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(bodyMap.get("errcode")) && Integer.parseInt(bodyMap.get("errcode").toString()) != 0) {
|
|
|
|
|
throw new RuntimeException("获取accessToken失败:" + bodyMap.get("errmsg"));
|
|
|
|
|
}
|
|
|
|
|
return bodyMap.get("accessToken").toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LarkInfoDTO getLarkInfo(String key) {
|
|
|
|
|
PlatformSource platformSource = platformSourceMapper.selectByPrimaryKey(key);
|
|
|
|
|
if (platformSource == null) {
|
|
|
|
|
return new LarkInfoDTO();
|
|
|
|
|
}
|
|
|
|
|
LarkInfoDTO LarkInfoDTO = new LarkInfoDTO();
|
|
|
|
|
LarkCreator larkCreator = JSON.parseObject(platformSource.getConfig(), LarkCreator.class);
|
|
|
|
|
BeanUtils.copyProperties(larkCreator, LarkInfoDTO);
|
|
|
|
|
LarkInfoDTO.setEnable(platformSource.getEnable());
|
|
|
|
|
LarkInfoDTO.setValid(platformSource.getValid());
|
|
|
|
|
return LarkInfoDTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String generateLarkAppToken(LarkInfoDTO vo, String url, String key) {
|
|
|
|
|
if (ObjectUtils.isEmpty(vo)) {
|
|
|
|
|
vo = getLarkInfo(key);
|
|
|
|
|
}
|
|
|
|
|
String agentId = vo.getAgentId();
|
|
|
|
|
String appSecret = vo.getAppSecret();
|
|
|
|
|
LarkBaseParamDTO larkBaseParamDTO = new LarkBaseParamDTO();
|
|
|
|
|
larkBaseParamDTO.setApp_id(agentId);
|
|
|
|
|
larkBaseParamDTO.setApp_secret(appSecret);
|
|
|
|
|
String body = qrCodeClient.postExchange(url, null, null, larkBaseParamDTO, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(bodyMap.get("code")) && Integer.parseInt(bodyMap.get("code").toString()) > 0) {
|
|
|
|
|
throw new RuntimeException("获取appAccessToken失败:" + bodyMap.get("msg"));
|
|
|
|
|
}
|
|
|
|
|
return bodyMap.get("app_access_token").toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String generateLarkUserToken(String authCode, String url, String larkAppToken) {
|
|
|
|
|
LarkTokenParamDTO larkTokenParamDTO = new LarkTokenParamDTO();
|
|
|
|
|
larkTokenParamDTO.setCode(authCode);
|
|
|
|
|
larkTokenParamDTO.setGrant_type("authorization_code");
|
|
|
|
|
String body = qrCodeClient.postExchange(url, "Bearer " + larkAppToken, HttpHeaders.AUTHORIZATION, larkTokenParamDTO, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
if (ObjectUtils.isNotEmpty(bodyMap.get("code")) && Integer.parseInt(bodyMap.get("code").toString()) > 0) {
|
|
|
|
|
throw new RuntimeException("获取user_access_token失败:" + bodyMap.get("msg"));
|
|
|
|
|
}
|
|
|
|
|
Object o = bodyMap.get("data");
|
|
|
|
|
return ((Map) o).get("access_token").toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> exchangeLarkToken(String authCode, WebSession session, Locale locale) {
|
|
|
|
|
LarkInfoDTO vo = getLarkInfo(LARK);
|
|
|
|
|
String larkAppToken = generateLarkAppToken(vo, LARK_APP_TOKEN_URL, LARK);
|
|
|
|
|
String larkUserToken = generateLarkUserToken(authCode, LARK_USER_TOKEN_URL, larkAppToken);
|
|
|
|
|
String body = qrCodeClient.exchange(LARK_USER_INFO_URL, "Bearer " + larkUserToken, HttpHeaders.AUTHORIZATION, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
Object uMapobj = bodyMap.get("data");
|
|
|
|
|
Map userMap = (Map) uMapobj;
|
|
|
|
|
String userId = userMap.get("union_id").toString();
|
|
|
|
|
String name = userMap.get("name").toString();
|
|
|
|
|
String email = ObjectUtils.isNotEmpty(userMap.get("email")) ? userMap.get("email").toString() : ObjectUtils.isNotEmpty(userMap.get("enterprise_email")) ? userMap.get("enterprise_email").toString() : "";
|
|
|
|
|
String mobile = ObjectUtils.isNotEmpty(userMap.get("mobile")) ? userMap.get("mobile").toString() : "";
|
|
|
|
|
UserRequest userCreateInfo = getUserRequest(userId, email, name, mobile);
|
|
|
|
|
return login(userCreateInfo, UserSource.QR_CODE.name(), session, locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Optional<SessionUser> exchangeLarkSuiteToken(String authCode, WebSession session, Locale locale) {
|
|
|
|
|
LarkInfoDTO vo = getLarkInfo(LARK_SUITE);
|
|
|
|
|
String larkAppToken = generateLarkAppToken(vo, LARK_SUITE_APP_TOKEN_URL, LARK);
|
|
|
|
|
String larkUserToken = generateLarkUserToken(authCode, LARK_SUITE_USER_TOKEN_URL, larkAppToken);
|
|
|
|
|
String body = qrCodeClient.exchange(LARK_SUITE_USER_INFO_URL, "Bearer " + larkUserToken, HttpHeaders.AUTHORIZATION, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON);
|
|
|
|
|
Map bodyMap = JSON.parseMap(body);
|
|
|
|
|
Object uMapobj = bodyMap.get("data");
|
|
|
|
|
Map userMap = (Map) uMapobj;
|
|
|
|
|
String userId = userMap.get("union_id").toString();
|
|
|
|
|
String name = userMap.get("name").toString();
|
|
|
|
|
String email = ObjectUtils.isNotEmpty(userMap.get("email")) ? userMap.get("email").toString() : ObjectUtils.isNotEmpty(userMap.get("enterprise_email")) ? userMap.get("enterprise_email").toString() : "";
|
|
|
|
|
String mobile = ObjectUtils.isNotEmpty(userMap.get("mobile")) ? userMap.get("mobile").toString() : "";
|
|
|
|
|
UserRequest userCreateInfo = getUserRequest(userId, email, name, mobile);
|
|
|
|
|
return login(userCreateInfo, UserSource.QR_CODE.name(), session, locale);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@NotNull
|
|
|
|
|
private static UserRequest getUserRequest(String userId, String email, String name, String mobile) {
|
|
|
|
|
UserRequest userCreateInfo = new UserRequest();
|
|
|
|
|
userCreateInfo.setId(userId);
|
|
|
|
|
userCreateInfo.setEmail(email);
|
|
|
|
|
userCreateInfo.setName(name);
|
|
|
|
|
userCreateInfo.setPhone(mobile);
|
|
|
|
|
return userCreateInfo;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|