-
Notifications
You must be signed in to change notification settings - Fork 1
Home
欢迎来到OAuth2说明文档
更多细节后续完善
一、Server-side flow url:https://oauth.****.com/authorize?response_type=code&client_id=23075594&redirect_uri=http://www.oauth.net/2/&state=1212&view=web
//获取code
OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request);
//生成授权码
String authorizationCode = null;
//responseType目前公支持CODE,另外还有TOKEN
String responseType = oauthRequest.getResponseType();
if (responseType.equals(ResponseType.CODE.toString())) {
//code生成
OAuthIssuerImpl oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
authorizationCode = oauthIssuerImpl.authorizationCode();
} else {
throw OAuthUtils.handleOAuthProblemException(OAuthState.unsupported_response_type);
}//获取token
OAuthTokenRequest oauthRequest = new OAuthTokenRequest(request);
// 检查验证类型,此处只检查AUTHORIZATION_CODE类型,其他的还有PASSWORD或REFRESH_TOKEN
String authCode = oauthRequest.getParam(OAuth.OAUTH_CODE);
if (oauthRequest.getParam(OAuth.OAUTH_GRANT_TYPE).equals(GrantType.AUTHORIZATION_CODE.toString())) {
if (!oAuthBPO.checkAuthCode(authCode)) {
throw OAuthUtils.handleOAuthProblemException(OAuthState.invalid_grant);
}
} else {
throw OAuthUtils.handleOAuthProblemException(OAuthState.unsupported_grant_type);
}
try {
OAuthSignatureMethod.newSigner(request).validate(request, credentials.getClientSecret());
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}二、Client-side flow 与Server-side flow一至,只是redirect_uri是平台提供
三、签名算法
为了防止API调用过程中被黑客恶意篡改,调用任何一个API都需要携带签名,TOP服务端会根据请求参数,对签名进行验证,签名不合法的请求将会被拒绝。TOP目前支持的签名算法有两种:MD5(sign_method=md5),HMAC_MD5(sign_method=hmac),签名大体过程如下:
对所有API请求参数(包括公共参数和业务参数,但除去sign参数和byte[]类型的参数),根据参数名称的ASCII码表的顺序排序。如:foo=1, bar=2, foo_bar=3, foobar=4排序后的顺序是bar=2, foo=1, foo_bar=3, foobar=4。 将排序好的参数名和参数值拼装在一起,根据上面的示例得到的结果为:bar2foo1foo_bar3foobar4。 把拼装好的字符串采用utf-8编码,使用签名算法对编码后的字节流进行摘要。如果使用MD5算法,则需要在拼装的字符串前后加上app的secret后,再进行摘要,如:md5(secret+bar2foo1foo_bar3foobar4+secret);如果使用HMAC_MD5算法,则需要用app的secret初始化摘要算法后,再进行摘要,如:hmac_md5(bar2foo1foo_bar3foobar4)。 将摘要得到的字节流结果使用十六进制表示,如:hex(“helloworld”.getBytes(“utf-8”)) = “68656C6C6F776F726C64”
说明:MD5和HMAC_MD5都是128位长度的摘要算法,用16进制表示,一个十六进制的字符能表示4个位,所以签名后的字符串长度固定为32个十六进制字符。