diff --git a/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Template.java b/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Template.java index 71b8b277..40a33246 100644 --- a/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Template.java +++ b/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Template.java @@ -364,6 +364,43 @@ public class SaOAuth2Template { } + // ----------------- Code 相关 ----------------- + + /** + * 获取 CodeModel,无效的 code 会返回 null + * @param code / + * @return / + */ + public CodeModel getCode(String code) { + return SaOAuth2Manager.getDao().getCode(code); + } + + /** + * 校验 Code,成功返回 CodeModel,失败则抛出异常 + * @param code / + * @return / + */ + public CodeModel checkCode(String code) { + CodeModel cm = SaOAuth2Manager.getDao().getCode(code); + if(cm == null) { + throw new SaOAuth2AuthorizationCodeException("无效 code: " + code) + .setAuthorizationCode(code) + .setCode(SaOAuth2ErrorCode.CODE_30110); + } + return cm; + } + + /** + * 获取 Code,根据索引: clientId、loginId + * @param clientId / + * @param loginId / + * @return / + */ + public String getCodeValue(String clientId, Object loginId) { + return SaOAuth2Manager.getDao().getCodeValue(clientId, loginId); + } + + // ----------------- Access-Token 相关 ----------------- /** diff --git a/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Util.java b/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Util.java index 17e7d88a..f0616f33 100644 --- a/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Util.java +++ b/sa-token-plugin/sa-token-oauth2/src/main/java/cn/dev33/satoken/oauth2/template/SaOAuth2Util.java @@ -16,9 +16,9 @@ package cn.dev33.satoken.oauth2.template; import cn.dev33.satoken.oauth2.SaOAuth2Manager; -import cn.dev33.satoken.oauth2.dao.SaOAuth2Dao; import cn.dev33.satoken.oauth2.data.model.AccessTokenModel; import cn.dev33.satoken.oauth2.data.model.ClientTokenModel; +import cn.dev33.satoken.oauth2.data.model.CodeModel; import cn.dev33.satoken.oauth2.data.model.RefreshTokenModel; import cn.dev33.satoken.oauth2.data.model.loader.SaClientModel; @@ -129,6 +129,37 @@ public class SaOAuth2Util { } + // ----------------- Code 相关 ----------------- + + /** + * 获取 CodeModel,无效的 code 会返回 null + * @param code / + * @return / + */ + public static CodeModel getCode(String code) { + return SaOAuth2Manager.getTemplate().getCode(code); + } + + /** + * 校验 Code,成功返回 CodeModel,失败则抛出异常 + * @param code / + * @return / + */ + public static CodeModel checkCode(String code) { + return SaOAuth2Manager.getTemplate().checkCode(code); + } + + /** + * 获取 Code,根据索引: clientId、loginId + * @param clientId / + * @param loginId / + * @return / + */ + public static String getCodeValue(String clientId, Object loginId) { + return SaOAuth2Manager.getTemplate().getCodeValue(clientId, loginId); + } + + // ----------------- Access-Token 相关 ----------------- /**