-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/me/zhyd/oauth/enums/scope/AuthDingTalkScope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package me.zhyd.oauth.enums.scope; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
/** | ||
* 钉钉平台 OAuth 授权范围 | ||
* | ||
* https://open.dingtalk.com/document/orgapp/obtain-identity-credentials#title-4up-u8w-5ug | ||
* | ||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) | ||
* @version 1.0.0 | ||
* @since 1.16.7 | ||
*/ | ||
@Getter | ||
@AllArgsConstructor | ||
public enum AuthDingTalkScope implements AuthScope { | ||
|
||
/** | ||
* 无需申请 默认开启 | ||
*/ | ||
openid("openid", "授权后可获得用户userid", true), | ||
/** | ||
* 无需申请 默认开启 | ||
*/ | ||
corpid("corpid", "授权后可获得登录过程中用户选择的组织id", false) | ||
; | ||
|
||
private final String scope; | ||
private final String description; | ||
private final boolean isDefault; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/main/java/me/zhyd/oauth/request/AuthDingTalkV2Request.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package me.zhyd.oauth.request; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import com.xkcoding.http.support.HttpHeader; | ||
import me.zhyd.oauth.cache.AuthStateCache; | ||
import me.zhyd.oauth.config.AuthConfig; | ||
import me.zhyd.oauth.config.AuthDefaultSource; | ||
import me.zhyd.oauth.enums.scope.AuthDingTalkScope; | ||
import me.zhyd.oauth.exception.AuthException; | ||
import me.zhyd.oauth.model.AuthCallback; | ||
import me.zhyd.oauth.model.AuthToken; | ||
import me.zhyd.oauth.model.AuthUser; | ||
import me.zhyd.oauth.utils.AuthScopeUtils; | ||
import me.zhyd.oauth.utils.HttpUtils; | ||
import me.zhyd.oauth.utils.UrlBuilder; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* 新版钉钉二维码登录 | ||
* | ||
* @author yadong.zhang (yadong.zhang0415(a)gmail.com) | ||
* @since 1.16.7 | ||
*/ | ||
public class AuthDingTalkV2Request extends AuthDefaultRequest { | ||
|
||
public AuthDingTalkV2Request(AuthConfig config) { | ||
super(config, AuthDefaultSource.DINGTALK_V2); | ||
} | ||
|
||
public AuthDingTalkV2Request(AuthConfig config, AuthStateCache authStateCache) { | ||
super(config, AuthDefaultSource.DINGTALK_V2, authStateCache); | ||
} | ||
|
||
@Override | ||
public String authorize(String state) { | ||
return UrlBuilder.fromBaseUrl(source.authorize()) | ||
.queryParam("response_type", "code") | ||
.queryParam("client_id", config.getClientId()) | ||
.queryParam("scope", this.getScopes(",", true, AuthScopeUtils.getDefaultScopes(AuthDingTalkScope.values()))) | ||
.queryParam("redirect_uri", config.getRedirectUri()) | ||
.queryParam("prompt", "consent") | ||
.queryParam("org_type", config.getDingTalkOrgType()) | ||
.queryParam("corpId", config.getDingTalkCorpId()) | ||
.queryParam("exclusiveLogin", config.isDingTalkExclusiveLogin()) | ||
.queryParam("exclusiveCorpId", config.getDingTalkExclusiveCorpId()) | ||
.queryParam("state", getRealState(state)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AuthToken getAccessToken(AuthCallback authCallback) { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put("grantType", "authorization_code"); | ||
params.put("clientId", config.getClientId()); | ||
params.put("clientSecret", config.getClientSecret()); | ||
params.put("code", authCallback.getCode()); | ||
String response = new HttpUtils(config.getHttpConfig()).post(this.source.accessToken(), JSONObject.toJSONString(params)).getBody(); | ||
JSONObject accessTokenObject = JSONObject.parseObject(response); | ||
if (!accessTokenObject.containsKey("accessToken")) { | ||
throw new AuthException(JSONObject.toJSONString(response), source); | ||
} | ||
return AuthToken.builder() | ||
.accessToken(accessTokenObject.getString("accessToken")) | ||
.refreshToken(accessTokenObject.getString("refreshToken")) | ||
.expireIn(accessTokenObject.getIntValue("expireIn")) | ||
.corpId(accessTokenObject.getString("corpId")) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public AuthUser getUserInfo(AuthToken authToken) { | ||
HttpHeader header = new HttpHeader(); | ||
header.add("x-acs-dingtalk-access-token", authToken.getAccessToken()); | ||
|
||
String response = new HttpUtils(config.getHttpConfig()).get(this.source.userInfo(), null, header, false).getBody(); | ||
JSONObject object = JSONObject.parseObject(response); | ||
|
||
authToken.setOpenId(object.getString("openId")); | ||
authToken.setUnionId(object.getString("unionId")); | ||
return AuthUser.builder() | ||
.rawUserInfo(object) | ||
.uuid(object.getString("unionId")) | ||
.username(object.getString("nick")) | ||
.nickname(object.getString("nick")) | ||
.avatar(object.getString("avatarUrl")) | ||
.snapshotUser(object.getBooleanValue("visitor")) | ||
.token(authToken) | ||
.source(source.toString()) | ||
.build(); | ||
} | ||
|
||
/** | ||
* 返回获取accessToken的url | ||
* | ||
* @param code 授权码 | ||
* @return 返回获取accessToken的url | ||
*/ | ||
protected String accessTokenUrl(String code) { | ||
return UrlBuilder.fromBaseUrl(source.accessToken()) | ||
.queryParam("code", code) | ||
.queryParam("clientId", config.getClientId()) | ||
.queryParam("clientSecret", config.getClientSecret()) | ||
.queryParam("grantType", "authorization_code") | ||
.build(); | ||
} | ||
} |