1
0
mirror of synced 2026-04-25 11:08:49 +08:00

🥚 添加 amazon 平台

This commit is contained in:
yadong.zhang
2021-03-13 23:10:48 +08:00
parent c79b97a0d0
commit 132a7f4338
7 changed files with 348 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package me.zhyd.oauth.utils;
import java.nio.charset.StandardCharsets;
/**
* 该配置仅用于支持 PKCE 模式的平台,针对无服务应用,不推荐使用隐式授权,推荐使用 PKCE 模式
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.0.0
*/
public class PkceUtil {
public static String generateCodeVerifier() {
String randomStr = RandomUtil.randomString(50);
return Base64Utils.encodeUrlSafe(randomStr);
}
/**
* 适用于 OAuth 2.0 PKCE 增强协议
*
* @param codeChallengeMethod s256 / plain
* @param codeVerifier 客户端生产的校验码
* @return code challenge
*/
public static String generateCodeChallenge(String codeChallengeMethod, String codeVerifier) {
if ("S256".equalsIgnoreCase(codeChallengeMethod)) {
// https://tools.ietf.org/html/rfc7636#section-4.2
// code_challenge = BASE64URL-ENCODE(SHA256(ASCII(code_verifier)))
return newStringUsAscii(Base64Utils.encodeUrlSafe(Sha256.digest(codeVerifier), true));
} else {
return codeVerifier;
}
}
public static String newStringUsAscii(byte[] bytes) {
return new String(bytes, StandardCharsets.US_ASCII);
}
}

View File

@@ -0,0 +1,38 @@
package me.zhyd.oauth.utils;
import java.util.concurrent.ThreadLocalRandom;
/**
* 生成随机字符串
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.16.0
*/
public class RandomUtil {
/**
* 用于随机选的字符和数字
*/
public static final String BASE_CHAR_NUMBER = "abcdefghijklmnopqrstuvwxyz0123456789";
/**
* 获得一个随机的字符串
*
* @param length 字符串的长度
* @return 指定长度的随机字符串
*/
public static String randomString(int length) {
final StringBuilder sb = new StringBuilder(length);
if (length < 1) {
length = 1;
}
int baseLength = BASE_CHAR_NUMBER.length();
for (int i = 0; i < length; i++) {
int number = ThreadLocalRandom.current().nextInt(baseLength);
sb.append(BASE_CHAR_NUMBER.charAt(number));
}
return sb.toString();
}
}

View File

@@ -0,0 +1,27 @@
package me.zhyd.oauth.utils;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* SHA256 加密
*
* @author yadong.zhang (yadong.zhang0415(a)gmail.com)
* @version 1.0.0
* @since 1.16.0
*/
public class Sha256 {
public static byte[] digest(String str) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
return messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}