提交 7df2c9af 编写于 作者: 智布道's avatar 智布道 👁

🎨 完成 github 的自定义 scope

上级 f2c1c2f0
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 AuthGithubScope implements AuthScope {
/**
* {@code scope} 含义,以{@code description} 为准
*/
REPO_STATUS("repo:status", "Grants read/write access to public and private repository commit statuses. This scope is only necessary to grant other users or services access to private repository commit statuses <em>without</em> granting access to the code.", false),
REPO_DEPLOYMENT("repo_deployment", "Grants access to deployment statuses for public and private repositories. This scope is only necessary to grant other users or services access to deployment statuses, <em>without</em> granting access to the code.", false),
PUBLIC_REPO("public_repo", "Limits access to public repositories. That includes read/write access to code, commit statuses, repository projects, collaborators, and deployment statuses for public repositories and organizations. Also required for starring public repositories.", false),
REPO_INVITE("repo:invite", "Grants accept/decline abilities for invitations to collaborate on a repository. This scope is only necessary to grant other users or services access to invites <em>without</em> granting access to the code.", false),
SECURITY_EVENTS("security_events", "Grants read and write access to security events in the code scanning API.", false),
WRITE_REPO_HOOK("write:repo_hook", "Grants read, write, and ping access to hooks in public or private repositories.", false),
READ_REPO_HOOK("read:repo_hook", "Grants read and ping access to hooks in public or private repositories.", false),
ADMIN_ORG("admin:org", "Fully manage the organization and its teams, projects, and memberships.", false),
WRITE_ORG("write:org", "Read and write access to organization membership, organization projects, and team membership.", false),
READ_ORG("read:org", "Read-only access to organization membership, organization projects, and team membership.", false),
ADMIN_PUBLIC_KEY("admin:public_key", "Fully manage public keys.", false),
WRITE_PUBLIC_KEY("write:public_key", "Create, list, and view details for public keys.", false),
READ_PUBLIC_KEY("read:public_key", "List and view details for public keys.", false),
GIST("gist", "Grants write access to gists.", false),
NOTIFICATIONS("notifications", "Grants: <br>* read access to a user's notifications <br>* mark as read access to threads <br>* watch and unwatch access to a repository, and <br>* read, write, and delete access to thread subscriptions.", false),
USER("user", "Grants read/write access to profile info only. Note that this scope includes <code>user:email</code> and <code>user:follow</code>.", false),
READ_USER("read:user", "Grants access to read a user's profile data.", false),
USER_EMAIL("user:email", "Grants read access to a user's email addresses.", false),
USER_FOLLOW("user:follow", "Grants access to follow or unfollow other users.", false),
DELETE_REPO("delete_repo", "Grants access to delete adminable repositories.", false),
WRITE_DISCUSSION("write:discussion", "Allows read and write access for team discussions.", false),
READ_DISCUSSION("read:discussion", "Allows read access for team discussions.", false),
WRITE_PACKAGES("write:packages", "Grants access to upload or publish a package in GitHub Packages. For more information, see \"<a href=\"https://help.github.com/github/managing-packages-with-github-packages/publishing-a-package\">Publishing a package</a>\" in the GitHub Help documentation.", false),
READ_PACKAGES("read:packages", "Grants access to download or install packages from GitHub Packages. For more information, see \"<a href=\"https://help.github.com/github/managing-packages-with-github-packages/installing-a-package\">Installing a package</a>\" in the GitHub Help documentation.", false),
DELETE_PACKAGES("delete:packages", "Grants access to delete packages from GitHub Packages. For more information, see \"<a href=\"https://help.github.com/github/managing-packages-with-github-packages/deleting-a-package\">Deleting packages</a>\" in the GitHub Help documentation.", false),
ADMIN_GPG_KEY("admin:gpg_key", "Fully manage GPG keys.", false),
WRITE_GPG_KEY("write:gpg_key", "Create, list, and view details for GPG keys.", false),
READ_GPG_KEY("read:gpg_key", "List and view details for GPG keys.", false),
WORKFLOW("workflow", "Grants the ability to add and update GitHub Actions workflow files. Workflow files can be committed without this scope if the same file (with both the same path and contents) exists on another branch in the same repository.", false),
;
private String scope;
private String description;
private boolean isDefault;
public static List<AuthScope> getDefaultScopes() {
AuthGithubScope[] scopes = AuthGithubScope.values();
List<AuthScope> defaultScopes = new ArrayList<>();
for (AuthGithubScope scope : scopes) {
if (scope.isDefault()) {
defaultScopes.add(scope);
}
}
return defaultScopes;
}
public static List<String> listScope() {
return Arrays.stream(AuthGithubScope.values()).map(AuthGithubScope::getScope).collect(Collectors.toList());
}
}
......@@ -278,10 +278,6 @@ public abstract class AuthDefaultRequest implements AuthRequest {
* @since 1.16.7
*/
protected String getScopes(String separator, boolean encode, List<AuthScope> defaultScopes) {
if (null == separator) {
// 默认为空格
separator = " ";
}
List<String> scopes = config.getScopes();
if (null == scopes || scopes.isEmpty()) {
if (null == defaultScopes || defaultScopes.isEmpty()) {
......@@ -292,6 +288,10 @@ public abstract class AuthDefaultRequest implements AuthRequest {
scopes.add(defaultScope.getScope());
}
}
if (null == separator) {
// 默认为空格
separator = " ";
}
String scopeStr = String.join(separator, scopes);
return encode ? UrlUtil.urlEncode(scopeStr) : scopeStr;
}
......
......@@ -5,11 +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.AuthGithubScope;
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.GlobalAuthUtils;
import me.zhyd.oauth.utils.UrlBuilder;
import java.util.Map;
......@@ -67,10 +69,24 @@ public class AuthGithubRequest extends AuthDefaultRequest {
.build();
}
private void checkResponse(boolean error, String error_description) {
private void checkResponse(boolean error, String errorDescription) {
if (error) {
throw new AuthException(error_description);
throw new AuthException(errorDescription);
}
}
/**
* 返回带{@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, AuthGithubScope.getDefaultScopes()))
.build();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册