From 753774b1931d766db958bb17445fe9372b5da25c Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Sat, 4 Jul 2020 12:52:55 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=AE=8C=E6=88=90=20Stackoverflow=20?= =?UTF-8?q?=E7=9A=84=E8=87=AA=E5=AE=9A=E4=B9=89=20scope?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../enums/scope/AuthStackoverflowScope.java | 48 +++++++++++++++++++ .../request/AuthStackOverflowRequest.java | 11 ++--- 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/main/java/me/zhyd/oauth/enums/scope/AuthStackoverflowScope.java diff --git a/src/main/java/me/zhyd/oauth/enums/scope/AuthStackoverflowScope.java b/src/main/java/me/zhyd/oauth/enums/scope/AuthStackoverflowScope.java new file mode 100644 index 0000000..b9f54c2 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/enums/scope/AuthStackoverflowScope.java @@ -0,0 +1,48 @@ +package me.zhyd.oauth.enums.scope; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Stackoverflow 平台 OAuth 授权范围 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.0 + */ +@Getter +@AllArgsConstructor +public enum AuthStackoverflowScope implements AuthScope { + + /** + * {@code scope} 含义,以{@code description} 为准 + */ + read_inbox("read_inbox", "access a user's global inbox", true), + NO_EXPIRY("no_expiry", "access_token's with this scope do not expire", false), + WRITE_ACCESS("write_access", "perform write operations as a user", false), + PRIVATE_INFO("private_info", "access full history of a user's private actions on the site", false); + + private String scope; + private String description; + private boolean isDefault; + + public static List getDefaultScopes() { + AuthStackoverflowScope[] scopes = AuthStackoverflowScope.values(); + List defaultScopes = new ArrayList<>(); + for (AuthStackoverflowScope scope : scopes) { + if (scope.isDefault()) { + defaultScopes.add(scope); + } + } + return defaultScopes; + } + + public static List listScope() { + return Arrays.stream(AuthStackoverflowScope.values()).map(AuthStackoverflowScope::getScope).collect(Collectors.toList()); + } +} diff --git a/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java b/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java index 9c301bb..750c7bd 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthStackOverflowRequest.java @@ -1,17 +1,18 @@ package me.zhyd.oauth.request; import com.alibaba.fastjson.JSONObject; -import me.zhyd.oauth.utils.HttpUtils; import com.xkcoding.http.constants.Constants; import com.xkcoding.http.support.HttpHeader; import com.xkcoding.http.util.MapUtil; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.enums.AuthUserGender; +import me.zhyd.oauth.enums.scope.AuthStackoverflowScope; 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.HttpUtils; import me.zhyd.oauth.utils.UrlBuilder; import java.util.Map; @@ -85,12 +86,8 @@ public class AuthStackOverflowRequest extends AuthDefaultRequest { */ @Override public String authorize(String state) { - return UrlBuilder.fromBaseUrl(source.authorize()) - .queryParam("response_type", "code") - .queryParam("client_id", config.getClientId()) - .queryParam("redirect_uri", config.getRedirectUri()) - .queryParam("scope", "read_inbox") - .queryParam("state", getRealState(state)) + return UrlBuilder.fromBaseUrl(super.authorize(state)) + .queryParam("scope", this.getScopes(",", false, AuthStackoverflowScope.getDefaultScopes())) .build(); } -- GitLab