|
@@ -8,7 +8,13 @@ import cn.dev33.satoken.oauth2.config.SaOAuth2Config;
|
|
|
import cn.dev33.satoken.oauth2.exception.SaOAuth2Exception;
|
|
|
import cn.dev33.satoken.oauth2.logic.SaOAuth2Consts;
|
|
|
import cn.dev33.satoken.oauth2.logic.SaOAuth2Handle;
|
|
|
+import cn.dev33.satoken.oauth2.logic.SaOAuth2Util;
|
|
|
+import cn.dev33.satoken.oauth2.model.AccessTokenModel;
|
|
|
+import cn.dev33.satoken.oauth2.model.ClientTokenModel;
|
|
|
+import cn.dev33.satoken.oauth2.model.RequestAuthModel;
|
|
|
import cn.dev33.satoken.oauth2.model.SaClientModel;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.dev33.satoken.util.SaResult;
|
|
|
|
|
|
public class TokenRequestHandler {
|
|
|
|
|
@@ -25,9 +31,9 @@ public class TokenRequestHandler {
|
|
|
return SaOAuth2Handle.authorize(req, res, cfg);
|
|
|
}
|
|
|
} else if (req.isPath(SaOAuth2Consts.Api.token) && req.isParam(SaOAuth2Consts.Param.grant_type, SaOAuth2Consts.GrantType.authorization_code)) {
|
|
|
- return SaOAuth2Handle.token(req, res, cfg);
|
|
|
+ return token(req, res, cfg);
|
|
|
} else if (req.isPath(SaOAuth2Consts.Api.token) && req.isParam(SaOAuth2Consts.Param.grant_type, SaOAuth2Consts.GrantType.refresh_token)) {
|
|
|
- return SaOAuth2Handle.refreshToken(req);
|
|
|
+ return refreshToken(req);
|
|
|
} else if (req.isPath(SaOAuth2Consts.Api.revoke)) {
|
|
|
return SaOAuth2Handle.revokeToken(req);
|
|
|
} else if (req.isPath(SaOAuth2Consts.Api.doLogin)) {
|
|
@@ -46,17 +52,65 @@ public class TokenRequestHandler {
|
|
|
if (!cfg.getIsPassword() || !cm.isPassword && !cm.isAutoMode) {
|
|
|
throw new SaOAuth2Exception("暂未开放的授权模式");
|
|
|
} else {
|
|
|
- return SaOAuth2Handle.password(req, res, cfg);
|
|
|
+ return password(req, res, cfg);
|
|
|
}
|
|
|
} else if (req.isPath(SaOAuth2Consts.Api.token) && req.isParam(SaOAuth2Consts.Param.grant_type, SaOAuth2Consts.GrantType.client_credentials)) {
|
|
|
cm = SaOAuth2Handle.currClientModel();
|
|
|
if (!cfg.getIsClient() || !cm.isClient && !cm.isAutoMode) {
|
|
|
throw new SaOAuth2Exception("暂未开放的授权模式");
|
|
|
} else {
|
|
|
- return SaOAuth2Handle.clientToken(req, res, cfg);
|
|
|
+ return clientToken(req, res, cfg);
|
|
|
}
|
|
|
} else {
|
|
|
return "{\"msg\": \"not handle\"}";
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public static Object token(SaRequest req, SaResponse res, SaOAuth2Config cfg) {
|
|
|
+ String code = req.getParamNotNull(SaOAuth2Consts.Param.code);
|
|
|
+ String clientId = req.getParamNotNull(SaOAuth2Consts.Param.client_id);
|
|
|
+ String clientSecret = req.getParamNotNull(SaOAuth2Consts.Param.client_secret);
|
|
|
+ String redirectUri = req.getParam(SaOAuth2Consts.Param.redirect_uri);
|
|
|
+ SaOAuth2Util.checkGainTokenParam(code, clientId, clientSecret, redirectUri);
|
|
|
+ AccessTokenModel token = SaOAuth2Util.generateAccessToken(code);
|
|
|
+ return token.toLineMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object refreshToken(SaRequest req) {
|
|
|
+ String clientId = req.getParamNotNull(SaOAuth2Consts.Param.client_id);
|
|
|
+ String clientSecret = req.getParamNotNull(SaOAuth2Consts.Param.client_secret);
|
|
|
+ String refreshToken = req.getParamNotNull(SaOAuth2Consts.Param.refresh_token);
|
|
|
+ SaOAuth2Util.checkRefreshTokenParam(clientId, clientSecret, refreshToken);
|
|
|
+ return SaOAuth2Util.refreshAccessToken(refreshToken).toLineMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object password(SaRequest req, SaResponse res, SaOAuth2Config cfg) {
|
|
|
+ String username = req.getParamNotNull(SaOAuth2Consts.Param.username);
|
|
|
+ String password = req.getParamNotNull(SaOAuth2Consts.Param.password);
|
|
|
+ String clientId = req.getParamNotNull(SaOAuth2Consts.Param.client_id);
|
|
|
+ String scope = req.getParam(SaOAuth2Consts.Param.scope, "");
|
|
|
+ SaOAuth2Util.checkContract(clientId, scope);
|
|
|
+ SaHolder.getStorage().set(StpUtil.stpLogic.splicingKeyJustCreatedSave(), "no-token");
|
|
|
+ Object retObj = cfg.getDoLoginHandle().apply(username, password);
|
|
|
+ if (!StpUtil.isLogin()) {
|
|
|
+ return retObj;
|
|
|
+ } else {
|
|
|
+ RequestAuthModel ra = new RequestAuthModel();
|
|
|
+ ra.clientId = clientId;
|
|
|
+ ra.loginId = StpUtil.getLoginId();
|
|
|
+ ra.scope = scope;
|
|
|
+ AccessTokenModel at = SaOAuth2Util.generateAccessToken(ra, true);
|
|
|
+ return at.toLineMap();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Object clientToken(SaRequest req, SaResponse res, SaOAuth2Config cfg) {
|
|
|
+ String clientId = req.getParamNotNull(SaOAuth2Consts.Param.client_id);
|
|
|
+ String clientSecret = req.getParamNotNull(SaOAuth2Consts.Param.client_secret);
|
|
|
+ String scope = req.getParam(SaOAuth2Consts.Param.scope);
|
|
|
+ SaOAuth2Util.checkContract(clientId, scope);
|
|
|
+ SaOAuth2Util.checkClientSecret(clientId, clientSecret);
|
|
|
+ ClientTokenModel ct = SaOAuth2Util.generateClientToken(clientId, scope);
|
|
|
+ return ct.toLineMap();
|
|
|
+ }
|
|
|
}
|