Skip to content

Commit

Permalink
💩 getAccessTokengetUserInfo两个方法从AuthDefaultRequest提升至`AuthRequ…
Browse files Browse the repository at this point in the history
…est`中,方便直接使用。[Github Issue#194](#194)
  • Loading branch information
zhangyd-c committed Aug 3, 2024
1 parent 15b1a97 commit 6d8b3b5
Show file tree
Hide file tree
Showing 46 changed files with 106 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public AbstractAuthDingtalkRequest(AuthConfig config, AuthSource source, AuthSta
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
return AuthToken.builder().accessCode(authCallback.getCode()).build();
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String code = authToken.getAccessCode();
JSONObject param = new JSONObject();
param.put("tmp_auth_code", code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AbstractAuthMicrosoftRequest(AuthConfig config, AuthSource source, AuthSt
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
return getToken(accessTokenUrl(authCallback.getCode()));
}

Expand Down Expand Up @@ -80,7 +80,7 @@ private void checkResponse(JSONObject object) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String token = authToken.getAccessToken();
String tokenType = authToken.getTokenType();
String jwt = tokenType + " " + token;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AbstractAuthWeChatEnterpriseRequest(AuthConfig config, AuthSource source,
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doGetAuthorizationCode(accessTokenUrl(null));

JSONObject object = this.checkResponse(response);
Expand All @@ -47,7 +47,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject object = this.checkResponse(response);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAfDianRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AuthAfDianRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> params = new HashMap<>();
params.put("grant_type", "authorization_code");
params.put("client_id", config.getClientId());
Expand All @@ -44,7 +44,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
return AuthUser.builder()
.uuid(authToken.getUserId())
.gender(AuthUserGender.UNKNOWN)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAlipayRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ protected void checkCode(AuthCallback authCallback) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(authCallback.getAuth_code());
Expand Down Expand Up @@ -200,7 +200,7 @@ public AuthResponse<AuthToken> refresh(AuthToken authToken) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAliyunRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AuthAliyunRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
return AuthToken.builder()
Expand All @@ -39,7 +39,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String userInfo = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(userInfo);
return AuthUser.builder()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAmazonRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public String authorize(String state) {
* @return access token
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(9);
form.put("grant_type", "authorization_code");
form.put("code", authCallback.getCode());
Expand Down Expand Up @@ -140,7 +140,7 @@ private void checkResponse(JSONObject jsonObject) {
* @return AuthUser
*/
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
this.checkToken(accessToken);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAppleRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public String authorize(String state) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
if (!StringUtils.isEmpty(authCallback.getError())) {
throw new AuthException(authCallback.getError());
}
Expand All @@ -76,7 +76,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
Base64.Decoder urlDecoder = Base64.getUrlDecoder();
String[] idToken = authToken.getIdToken().split("\\.");
String payload = new String(urlDecoder.decode(idToken[1]));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthBaiduRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public AuthBaiduRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
return getAuthToken(response);
}
Expand All @@ -48,7 +48,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
* @return AuthUser
*/
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String userInfo = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthCodingRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AuthCodingRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doGetAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
Expand All @@ -42,7 +42,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthCsdnRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public AuthCsdnRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
return AuthToken.builder().accessToken(accessTokenObject.getString("access_token")).build();
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response);
this.checkResponse(object);
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,6 @@ public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache a
this.checkConfig(config);
}

/**
* 获取access token
*
* @param authCallback 授权成功后的回调参数
* @return token
* @see AuthDefaultRequest#authorize()
* @see AuthDefaultRequest#authorize(String)
*/
protected abstract AuthToken getAccessToken(AuthCallback authCallback);

/**
* 使用token换取用户信息
*
* @param authToken token信息
* @return 用户信息
* @see AuthDefaultRequest#getAccessToken(AuthCallback)
*/
protected abstract AuthUser getUserInfo(AuthToken authToken);

/**
* 统一的登录入口。当通过{@link AuthDefaultRequest#authorize(String)}授权成功后,会跳转到调用方的相关回调方法中
* 方法的入参可以使用{@code AuthCallback},{@code AuthCallback}类中封装好了OAuth2授权回调所需要的参数
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthDouyinRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public AuthDouyinRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
return this.getToken(accessTokenUrl(authCallback.getCode()));
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject userInfoObject = JSONObject.parseObject(response);
this.checkResponse(userInfoObject);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AuthElemeRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(7);
form.put("client_id", config.getClientId());
form.put("redirect_uri", config.getRedirectUri());
Expand All @@ -66,7 +66,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
Map<String, Object> parameters = new HashMap<>(4);
// 获取商户账号信息的API接口名称
String action = "eleme.user.getUser";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthFacebookRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AuthFacebookRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
Expand All @@ -44,7 +44,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String userInfo = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthFeishuRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private String getAppAccessToken() {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
JSONObject requestObject = new JSONObject();
requestObject.put("app_access_token", this.getAppAccessToken());
requestObject.put("grant_type", "authorization_code");
Expand All @@ -76,7 +76,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
String response = new HttpUtils(config.getHttpConfig()).get(source.userInfo(), null, new HttpHeader()
.add("Content-Type", "application/json")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthFigmaRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String authorize(String state) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
HttpHeader header = new HttpHeader()
.add("content-type", "application/x-www-form-urlencoded")
.add("Authorization", "Basic " + Base64Utils.encode(config.getClientId().concat(":").concat(config.getClientSecret())));
Expand Down Expand Up @@ -86,7 +86,7 @@ protected String refreshTokenUrl(String refreshToken) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
HttpHeader header = new HttpHeader().add("Authorization", "Bearer " + authToken.getAccessToken());
String response = new HttpUtils(config.getHttpConfig()).get(super.userInfoUrl(authToken), null, header, false).getBody();
JSONObject dataObj = JSONObject.parseObject(response);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AuthGiteeRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
Expand All @@ -44,7 +44,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String userInfo = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(userInfo);
this.checkResponse(object);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthGithubRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AuthGithubRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
Map<String, String> res = GlobalAuthUtils.parseStringToMap(response);

Expand All @@ -49,7 +49,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
HttpHeader header = new HttpHeader();
header.add("Authorization", "token " + authToken.getAccessToken());
String response = new HttpUtils(config.getHttpConfig()).get(UrlBuilder.fromBaseUrl(source.userInfo()).build(), null, header, false).getBody();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthGitlabRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AuthGitlabRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject object = JSONObject.parseObject(response);

Expand All @@ -46,7 +46,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
String response = doGetUserInfo(authToken);
JSONObject object = JSONObject.parseObject(response);

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthGoogleRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AuthGoogleRequest(AuthConfig config, AuthStateCache authStateCache) {
}

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
String response = doPostAuthorizationCode(authCallback.getCode());
JSONObject accessTokenObject = JSONObject.parseObject(response);
this.checkResponse(accessTokenObject);
Expand All @@ -47,7 +47,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
HttpHeader httpHeader = new HttpHeader();
httpHeader.add("Authorization", "Bearer " + authToken.getAccessToken());
String userInfo = new HttpUtils(config.getHttpConfig()).post(userInfoUrl(authToken), null, httpHeader).getBody();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthHuaweiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public AuthHuaweiRequest(AuthConfig config, AuthStateCache authStateCache) {
* @see AuthDefaultRequest#authorize(String)
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
public AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(8);
form.put("grant_type", "authorization_code");
form.put("code", authCallback.getAuthorization_code());
Expand All @@ -66,7 +66,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
* @see AuthDefaultRequest#getAccessToken(AuthCallback)
*/
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
public AuthUser getUserInfo(AuthToken authToken) {
Map<String, String> form = new HashMap<>(7);
form.put("nsp_ts", System.currentTimeMillis() + "");
form.put("access_token", authToken.getAccessToken());
Expand Down
Loading

0 comments on commit 6d8b3b5

Please sign in to comment.