Skip to content

Commit

Permalink
🎨 完成 weibo 的自定义 scope
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c committed Jul 4, 2020
1 parent 753774b commit e9fa31b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
53 changes: 53 additions & 0 deletions src/main/java/me/zhyd/oauth/enums/scope/AuthWeiboScope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 AuthWeiboScope implements AuthScope {

/**
* {@code scope} 含义,以{@code description} 为准
*/
ALL("all", "获取所有权限", true),
EMAIL("email", "用户的联系邮箱,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/2/account/profile/email\">接口文档</a>", false),
DIRECT_MESSAGES_WRITE("direct_messages_write", "私信发送接口,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/C/2/direct_messages/send\">接口文档</a>", false),
DIRECT_MESSAGES_READ("direct_messages_read", "私信读取接口,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/C/2/direct_messages\">接口文档</a>", false),
INVITATION_WRITE("invitation_write", "邀请发送接口,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/Messages#.E5.A5.BD.E5.8F.8B.E9.82.80.E8.AF.B7\">接口文档</a>", false),
FRIENDSHIPS_GROUPS_READ("friendships_groups_read", "好友分组读取接口组,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2#.E5.A5.BD.E5.8F.8B.E5.88.86.E7.BB.84\">接口文档</a>", false),
FRIENDSHIPS_GROUPS_WRITE("friendships_groups_write", "好友分组写入接口组,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2#.E5.A5.BD.E5.8F.8B.E5.88.86.E7.BB.84\">接口文档</a>", false),
STATUSES_TO_ME_READ("statuses_to_me_read", "定向微博读取接口组,<a rel=\"nofollow\" href=\"http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2#.E5.BE.AE.E5.8D.9A\">接口文档</a>", false),
FOLLOW_APP_OFFICIAL_MICROBLOG("follow_app_official_microblog", "关注应用官方微博,该参数不对应具体接口,只需在应用控制台填写官方帐号即可。填写的路径:我的应用-选择自己的应用-应用信息-基本信息-官方运营账号(默认值是应用开发者帐号)", false);

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

public static List<AuthScope> getDefaultScopes() {
AuthWeiboScope[] scopes = AuthWeiboScope.values();
List<AuthScope> defaultScopes = new ArrayList<>();
for (AuthWeiboScope scope : scopes) {
if (scope.isDefault()) {
defaultScopes.add(scope);
}
}
return defaultScopes;
}

public static List<String> listScope() {
return Arrays.stream(AuthWeiboScope.values()).map(AuthWeiboScope::getScope).collect(Collectors.toList());
}
}
10 changes: 9 additions & 1 deletion src/main/java/me/zhyd/oauth/request/AuthWeiboRequest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package me.zhyd.oauth.request;

import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.utils.HttpUtils;
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.AuthUserGender;
import me.zhyd.oauth.enums.scope.AuthWeiboScope;
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.HttpUtils;
import me.zhyd.oauth.utils.IpUtils;
import me.zhyd.oauth.utils.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder;
Expand Down Expand Up @@ -93,6 +94,13 @@ protected String userInfoUrl(AuthToken authToken) {
.build();
}

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

@Override
public AuthResponse revoke(AuthToken authToken) {
String response = doGetRevoke(authToken);
Expand Down

0 comments on commit e9fa31b

Please sign in to comment.