🎨 facebook 支持自定义 scope
This commit is contained in:
@@ -6,6 +6,7 @@ import lombok.Getter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 边度平台 OAuth 授权范围
|
||||
@@ -42,7 +43,7 @@ public enum AuthBaiduScope implements AuthScope {
|
||||
return defaultScopes;
|
||||
}
|
||||
|
||||
public static List<AuthScope> listAll() {
|
||||
return Arrays.asList(AuthBaiduScope.values());
|
||||
public static List<String> listScope() {
|
||||
return Arrays.stream(AuthBaiduScope.values()).map(AuthBaiduScope::getScope).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import lombok.Getter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Coding平台 OAuth 授权范围
|
||||
@@ -43,7 +44,7 @@ public enum AuthCodingScope implements AuthScope {
|
||||
return defaultScopes;
|
||||
}
|
||||
|
||||
public static List<AuthScope> listAll() {
|
||||
return Arrays.asList(AuthCodingScope.values());
|
||||
public static List<String> listScope() {
|
||||
return Arrays.stream(AuthCodingScope.values()).map(AuthCodingScope::getScope).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ public class AuthBaiduRequest extends AuthDefaultRequest {
|
||||
.queryParam("client_id", config.getClientId())
|
||||
.queryParam("redirect_uri", config.getRedirectUri())
|
||||
.queryParam("display", "popup")
|
||||
.queryParam("scope", this.getScopes(" ", false, AuthBaiduScope.getDefaultScopes()))
|
||||
.queryParam("scope", this.getScopes(" ", true, AuthBaiduScope.getDefaultScopes()))
|
||||
.queryParam("state", getRealState(state))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -281,18 +282,17 @@ public abstract class AuthDefaultRequest implements AuthRequest {
|
||||
// 默认为空格
|
||||
separator = " ";
|
||||
}
|
||||
List<AuthScope> scopes = config.getScopes();
|
||||
List<String> scopes = config.getScopes();
|
||||
if (null == scopes || scopes.isEmpty()) {
|
||||
if (null == defaultScopes || defaultScopes.isEmpty()) {
|
||||
return null;
|
||||
return "";
|
||||
}
|
||||
scopes = new ArrayList<>();
|
||||
for (AuthScope defaultScope : defaultScopes) {
|
||||
scopes.add(defaultScope.getScope());
|
||||
}
|
||||
scopes = defaultScopes;
|
||||
}
|
||||
StringBuilder res = new StringBuilder();
|
||||
for (AuthScope scope : scopes) {
|
||||
res.append(scope.getScope()).append(separator);
|
||||
}
|
||||
String scopeStr = res.deleteCharAt(res.length() - separator.length()).toString();
|
||||
String scopeStr = String.join(separator, scopes);
|
||||
return encode ? UrlUtil.urlEncode(scopeStr) : scopeStr;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ 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.exception.AuthException;
|
||||
import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
@@ -95,4 +96,18 @@ public class AuthFacebookRequest extends AuthDefaultRequest {
|
||||
throw new AuthException(object.getJSONObject("error").getString("message"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回带{@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(",", false, AuthFacebookScope.getDefaultScopes()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user