diff --git a/src/main/java/me/zhyd/oauth/enums/scope/AuthKujialeScope.java b/src/main/java/me/zhyd/oauth/enums/scope/AuthKujialeScope.java new file mode 100644 index 0000000000000000000000000000000000000000..f7cb38e5f5653fee653ea379e8d75490dee936b0 --- /dev/null +++ b/src/main/java/me/zhyd/oauth/enums/scope/AuthKujialeScope.java @@ -0,0 +1,47 @@ +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; + +/** + * 酷家乐平台 OAuth 授权范围 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.0 + */ +@Getter +@AllArgsConstructor +public enum AuthKujialeScope implements AuthScope { + + /** + * {@code scope} 含义,以{@code description} 为准 + */ + GET_USER_INFO("get_user_info", "获取用户的基本信息", true), + GET_DESIGN("get_design", "获取指定方案详情", false), + GET_BUDGET_LIST("get_budget_list", "获取清单预算概览数据", false); + + private String scope; + private String description; + private boolean isDefault; + + public static List getDefaultScopes() { + AuthKujialeScope[] scopes = AuthKujialeScope.values(); + List defaultScopes = new ArrayList<>(); + for (AuthKujialeScope scope : scopes) { + if (scope.isDefault()) { + defaultScopes.add(scope); + } + } + return defaultScopes; + } + + public static List listScope() { + return Arrays.stream(AuthKujialeScope.values()).map(AuthKujialeScope::getScope).collect(Collectors.toList()); + } +} diff --git a/src/main/java/me/zhyd/oauth/request/AuthKujialeRequest.java b/src/main/java/me/zhyd/oauth/request/AuthKujialeRequest.java index d0e751bfa5faf0019e89733496de3091aae9276d..beaebda9fe230530e6bee30cdd829586c7eb65ed 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthKujialeRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthKujialeRequest.java @@ -1,17 +1,17 @@ package me.zhyd.oauth.request; import com.alibaba.fastjson.JSONObject; -import me.zhyd.oauth.utils.HttpUtils; 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.AuthKujialeScope; 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.StringUtils; +import me.zhyd.oauth.utils.HttpUtils; import me.zhyd.oauth.utils.UrlBuilder; /** @@ -40,27 +40,10 @@ public class AuthKujialeRequest extends AuthDefaultRequest { */ @Override public String authorize(String state) { - return authorize(state, "get_user_info"); - } - - /** - * 请求授权url - * - * @param state state 验证授权流程的参数,可以防止csrf - * @param scopeStr 请求用户授权时向用户显示的可进行授权的列表。如果要填写多个接口名称,请用逗号隔开 - * 参考https://open.kujiale.com/open/apps/2/docs?doc_id=95#Step1%EF%BC%9A%E8%8E%B7%E5%8F%96Authorization%20Code参数表内的scope字段 - * @return authorize url - */ - public String authorize(String state, String scopeStr) { - UrlBuilder urlBuilder = UrlBuilder.fromBaseUrl(source.authorize()) - .queryParam("response_type", "code") - .queryParam("client_id", config.getClientId()) - .queryParam("redirect_uri", config.getRedirectUri()) - .queryParam("state", getRealState(state)); - if (StringUtils.isNotEmpty(scopeStr)) { - urlBuilder.queryParam("scope", scopeStr); - } - return urlBuilder.build(); + String authorizeUrl = super.authorize(state); + return UrlBuilder.fromBaseUrl(authorizeUrl) + .queryParam("scope", this.getScopes(",", false, AuthKujialeScope.getDefaultScopes())) + .build(); } @Override