Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
  • Loading branch information
zhangyd-c committed Aug 3, 2024
2 parents bf5a6d1 + a1c1ab9 commit 6cc4149
Show file tree
Hide file tree
Showing 29 changed files with 230 additions and 55 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<maven-gpg-version>1.6</maven-gpg-version>
<maven.test.skip>false</maven.test.skip>
<simple-http.version>1.0.5</simple-http.version>
<lombok-version>1.18.20</lombok-version>
<lombok-version>1.18.30</lombok-version>
<junit-version>4.13.2</junit-version>
<fastjson-version>1.2.83</fastjson-version>
<alipay-sdk-version>4.17.5.ALL</alipay-sdk-version>
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/me/zhyd/oauth/config/AuthDefaultSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,33 @@ public String userInfo() {
public Class<? extends AuthDefaultRequest> getTargetClass() {
return AuthAppleRequest.class;
}
},

FIGMA{
@Override
public String authorize() {
return "https://www.figma.com/oauth";
}

@Override
public String accessToken() {
return "https://www.figma.com/api/oauth/token";
}

@Override
public String userInfo() {
return "https://api.figma.com/v1/me";
}

@Override
public String refresh() {
return "https://www.figma.com/api/oauth/refresh";
}

@Override
public Class<? extends AuthDefaultRequest> getTargetClass() {
return null;
}
}

}
29 changes: 29 additions & 0 deletions src/main/java/me/zhyd/oauth/enums/scope/AuthFigmaScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.zhyd.oauth.enums.scope;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
* Figma OAuth 授权范围
* <a href="https://www.figma.com/developers/api#authentication-scopes">...</a>
*
* @author xiangqian
* @since 1.16.6
*/
@Getter
@AllArgsConstructor
public enum AuthFigmaScope implements AuthScope {

FILE_CONTENT("files:read", "Read files, projects, users, versions, comments, components & styles, and webhooks", true),
VARIABLES("file_variables:read,file_variables:write", "Read and write to variables in Figma file. Note: this is only available to members in Enterprise organizations", false),
COMMENTS("file_comments:write", "Post and delete comments and comment reactions in files", false),
DEV_RESOURCES("file_dev_resources:read,file_dev_resources:write", "Read and write to dev resources in files", false),
LIBRARY_ANALYTICS("library_analytics:read", "Read your design system analytics", false),
WEBHOOKS("webhooks:write", "Create and manage webhooks", false);

private final String scope;
private final String description;
private final boolean isDefault;


}
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected AuthUser getUserInfo(AuthToken authToken) {
* @return AuthResponse
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
return AuthResponse.builder()
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(getToken(refreshTokenUrl(authToken.getRefreshToken())))
.build();
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 @@ -175,7 +175,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
* @return AuthResponse
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("refresh_token");
request.setRefreshToken(authToken.getRefreshToken());
Expand All @@ -188,7 +188,7 @@ public AuthResponse refresh(AuthToken authToken) {
if (!response.isSuccess()) {
throw new AuthException(response.getSubMsg());
}
return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(response.getAccessToken())
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 @@ -94,13 +94,13 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
}

@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
Map<String, String> form = new HashMap<>(7);
form.put("grant_type", "refresh_token");
form.put("refresh_token", authToken.getRefreshToken());
form.put("client_id", config.getClientId());
form.put("client_secret", config.getClientSecret());
return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(getToken(form, this.source.refresh()))
.build();
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 @@ -81,15 +81,15 @@ public AuthResponse revoke(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
String refreshUrl = UrlBuilder.fromBaseUrl(this.source.refresh())
.queryParam("grant_type", "refresh_token")
.queryParam("refresh_token", authToken.getRefreshToken())
.queryParam("client_id", this.config.getClientId())
.queryParam("client_secret", this.config.getClientSecret())
.build();
String response = new HttpUtils(config.getHttpConfig()).get(refreshUrl).getBody();
return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(this.getAuthToken(response))
.build();
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/me/zhyd/oauth/request/AuthDefaultRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public AuthDefaultRequest(AuthConfig config, AuthSource source, AuthStateCache a
* @return AuthResponse
*/
@Override
public AuthResponse login(AuthCallback authCallback) {
public AuthResponse<AuthUser> login(AuthCallback authCallback) {
try {
checkCode(authCallback);
if (!config.isIgnoreCheckState()) {
Expand All @@ -79,7 +79,7 @@ public AuthResponse login(AuthCallback authCallback) {

AuthToken authToken = this.getAccessToken(authCallback);
AuthUser user = this.getUserInfo(authToken);
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
return AuthResponse.<AuthUser>builder().code(AuthResponseStatus.SUCCESS.getCode()).data(user).build();
} catch (Exception e) {
Log.error("Failed to login with oauth authorization.", e);
return this.responseError(e);
Expand All @@ -96,7 +96,7 @@ protected void checkCode(AuthCallback authCallback) {
* @param e 具体的异常
* @return AuthResponse
*/
AuthResponse responseError(Exception e) {
AuthResponse<AuthUser> responseError(Exception e) {
int errorCode = AuthResponseStatus.FAILURE.getCode();
String errorMsg = e.getMessage();
if (e instanceof AuthException) {
Expand All @@ -106,7 +106,7 @@ AuthResponse responseError(Exception e) {
errorMsg = authException.getErrorMsg();
}
}
return AuthResponse.builder().code(errorCode).msg(errorMsg).build();
return AuthResponse.<AuthUser>builder().code(errorCode).msg(errorMsg).build();
}

/**
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 @@ -59,8 +59,8 @@ protected AuthUser getUserInfo(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken oldToken) {
return AuthResponse.builder()
public AuthResponse<AuthToken> refresh(AuthToken oldToken) {
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(getToken(refreshTokenUrl(oldToken.getRefreshToken())))
.build();
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 @@ -117,7 +117,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken oldToken) {
public AuthResponse<AuthToken> refresh(AuthToken oldToken) {
Map<String, String> form = new HashMap<>(4);
form.put("refresh_token", oldToken.getRefreshToken());
form.put("grant_type", "refresh_token");
Expand All @@ -129,7 +129,7 @@ public AuthResponse refresh(AuthToken oldToken) {

this.checkResponse(object);

return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(object.getString("access_token"))
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 @@ -98,12 +98,12 @@ protected AuthUser getUserInfo(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
JSONObject requestObject = new JSONObject();
requestObject.put("app_access_token", this.getAppAccessToken());
requestObject.put("grant_type", "refresh_token");
requestObject.put("refresh_token", authToken.getRefreshToken());
return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(getToken(requestObject, this.source.refresh()))
.build();
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/me/zhyd/oauth/request/AuthFigmaRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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.AuthResponseStatus;
import me.zhyd.oauth.enums.scope.AuthFigmaScope;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.utils.*;

/**
* Figma登录
* @author xiangqian
* @since 1.16.6
*/
public class AuthFigmaRequest extends AuthDefaultRequest {
public AuthFigmaRequest(AuthConfig config) {
super(config, AuthDefaultSource.FIGMA);
}

public AuthFigmaRequest(AuthConfig config, AuthStateCache authStateCache) {
super(config, AuthDefaultSource.FIGMA, authStateCache);
}

@Override
public String authorize(String state) {
return UrlBuilder.fromBaseUrl(super.authorize(state))
.queryParam("scope", this.getScopes(",", true, AuthScopeUtils.getDefaultScopes(AuthFigmaScope.values())))
.build();
}

@Override
protected 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())));

String response = new HttpUtils(config.getHttpConfig()).post(super.accessTokenUrl(authCallback.getCode()), null, header, true).getBody();
JSONObject accessTokenObject = JSONObject.parseObject(response);

this.checkResponse(accessTokenObject);

return AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
.refreshToken(accessTokenObject.getString("refresh_token"))
.scope(accessTokenObject.getString("scope"))
.userId(accessTokenObject.getString("user_id"))
.expireIn(accessTokenObject.getIntValue("expires_in"))
.build();
}

@Override
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
HttpHeader header = new HttpHeader().add("content-type", "application/x-www-form-urlencoded");
String response = new HttpUtils(config.getHttpConfig()).post(this.refreshTokenUrl(authToken.getRefreshToken()), null, header, false).getBody();
JSONObject dataObj = JSONObject.parseObject(response);

this.checkResponse(dataObj);

return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(dataObj.getString("access_token"))
.openId(dataObj.getString("open_id"))
.expireIn(dataObj.getIntValue("expires_in"))
.refreshToken(dataObj.getString("refresh_token"))
.scope(dataObj.getString("scope"))
.build())
.build();

}

@Override
protected String refreshTokenUrl(String refreshToken) {
return UrlBuilder.fromBaseUrl(source.refresh())
.queryParam("client_id", config.getClientId())
.queryParam("client_secret", config.getClientSecret())
.queryParam("refresh_token", refreshToken)
.build();
}

@Override
protected 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);

this.checkResponse(dataObj);

return AuthUser.builder()
.rawUserInfo(dataObj)
.uuid(dataObj.getString("id"))
.username(dataObj.getString("handle"))
.avatar(dataObj.getString("img_url"))
.email(dataObj.getString("email"))
.token(authToken)
.source(source.toString())
.build();
}


/**
* 校验响应结果
*
* @param object 接口返回的结果
*/
private void checkResponse(JSONObject object) {
if (object.containsKey("error")) {
throw new AuthException(object.getString("error") + ":" + object.getString("message"));
}
}
}
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 @@ -99,15 +99,15 @@ protected AuthUser getUserInfo(AuthToken authToken) {
* @return AuthResponse
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
Map<String, String> form = new HashMap<>(7);
form.put("client_id", config.getClientId());
form.put("client_secret", config.getClientSecret());
form.put("refresh_token", authToken.getRefreshToken());
form.put("grant_type", "refresh_token");

String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false).getBody();
return AuthResponse.builder().code(SUCCESS.getCode()).data(getAuthToken(response)).build();
return AuthResponse.<AuthToken>builder().code(SUCCESS.getCode()).data(getAuthToken(response)).build();
}

private AuthToken getAuthToken(String response) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthJdRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private JSONObject getUserDataJsonObject(JSONObject object) {
}

@Override
public AuthResponse refresh(AuthToken oldToken) {
public AuthResponse<AuthToken> refresh(AuthToken oldToken) {
Map<String, String> params = new HashMap<>(7);
params.put("app_key", config.getClientId());
params.put("app_secret", config.getClientSecret());
Expand All @@ -114,7 +114,7 @@ public AuthResponse refresh(AuthToken oldToken) {

this.checkResponse(object);

return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(object.getString("access_token"))
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthKujialeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ private String getOpenId(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken authToken) {
public AuthResponse<AuthToken> refresh(AuthToken authToken) {
String response = new HttpUtils(config.getHttpConfig()).post(refreshTokenUrl(authToken.getRefreshToken())).getBody();
return AuthResponse.builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build();
return AuthResponse.<AuthToken>builder().code(AuthResponseStatus.SUCCESS.getCode()).data(getAuthToken(response)).build();
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthLineRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ public AuthResponse revoke(AuthToken authToken) {
}

@Override
public AuthResponse refresh(AuthToken oldToken) {
public AuthResponse<AuthToken> refresh(AuthToken oldToken) {
Map<String, String> params = new HashMap<>();
params.put("grant_type", "refresh_token");
params.put("refresh_token", oldToken.getRefreshToken());
params.put("client_id", config.getClientId());
params.put("client_secret", config.getClientSecret());
String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), params, false).getBody();
JSONObject accessTokenObject = JSONObject.parseObject(response);
return AuthResponse.builder()
return AuthResponse.<AuthToken>builder()
.code(AuthResponseStatus.SUCCESS.getCode())
.data(AuthToken.builder()
.accessToken(accessTokenObject.getString("access_token"))
Expand Down
Loading

0 comments on commit 6cc4149

Please sign in to comment.