diff --git a/src/main/java/me/zhyd/oauth/enums/scope/AuthGiteeScope.java b/src/main/java/me/zhyd/oauth/enums/scope/AuthGiteeScope.java new file mode 100644 index 0000000000000000000000000000000000000000..f63b1f49c09e21f6ecff40d06e73c57e227bdb7a --- /dev/null +++ b/src/main/java/me/zhyd/oauth/enums/scope/AuthGiteeScope.java @@ -0,0 +1,55 @@ +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; + +/** + * Gitee 平台 OAuth 授权范围 + * + * @author yadong.zhang (yadong.zhang0415(a)gmail.com) + * @version 1.0.0 + * @since 1.0.0 + */ +@Getter +@AllArgsConstructor +public enum AuthGiteeScope implements AuthScope { + + /** + * {@code scope} 含义,以{@code description} 为准 + */ + USER_INFO("user_info", "访问用户的个人信息、最新动态等", true), + PROJECTS("projects", "查看、创建、更新用户的项目", false), + PULL_REQUESTS("pull_requests", "查看、发布、更新用户的 Pull Request", false), + ISSUES("issues", "查看、发布、更新用户的 Issue", false), + NOTES("notes", "查看、发布、管理用户在项目、代码片段中的评论", false), + KEYS("keys", "查看、部署、删除用户的公钥", false), + HOOK("hook", "查看、部署、更新用户的 Webhook", false), + GROUPS("groups", "查看、管理用户的组织以及成员", false), + GISTS("gists", "查看、删除、更新用户的代码片段", false), + ENTERPRISES("enterprises", "查看、管理用户的企业以及成员", false), + EMAILS("emails", "查看用户的个人邮箱信息", false); + + private String scope; + private String description; + private boolean isDefault; + + public static List getDefaultScopes() { + AuthGiteeScope[] scopes = AuthGiteeScope.values(); + List defaultScopes = new ArrayList<>(); + for (AuthGiteeScope scope : scopes) { + if (scope.isDefault()) { + defaultScopes.add(scope); + } + } + return defaultScopes; + } + + public static List listScope() { + return Arrays.stream(AuthGiteeScope.values()).map(AuthGiteeScope::getScope).collect(Collectors.toList()); + } +} diff --git a/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java b/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java index 0a21cecbc90ffd7276217f239ed6e3097cbec739..8dfc6e64affebcaa37a87b24d743774ef551e053 100644 --- a/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java +++ b/src/main/java/me/zhyd/oauth/request/AuthGiteeRequest.java @@ -5,10 +5,13 @@ import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.enums.AuthUserGender; +import me.zhyd.oauth.enums.scope.AuthFacebookScope; +import me.zhyd.oauth.enums.scope.AuthGiteeScope; 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.UrlBuilder; /** * Gitee登录 @@ -72,4 +75,18 @@ public class AuthGiteeRequest extends AuthDefaultRequest { throw new AuthException(object.getString("error_description")); } } + + /** + * 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state} + * + * @param state state 验证授权流程的参数,可以防止csrf + * @return 返回授权地址 + */ + @Override + public String authorize(String state) { + String authorizeUrl = super.authorize(state); + return UrlBuilder.fromBaseUrl(authorizeUrl) + .queryParam("scope", this.getScopes(" ", true, AuthGiteeScope.getDefaultScopes())) + .build(); + } }