/// <summary> /// 从Http请求中解析授权请求的信息 /// </summary> /// <param name="request">Http请求</param> /// <returns>授权请求对象</returns> public static AuthRequest ParseAuthRequest(HttpRequestBase request) { Arguments.NotNull(request, "request"); AccountType accountType; if (!AccountTypeExtend.TryParse(GetString(request, Protocal.ACCOUNT_TYPE), out accountType)) { throw new OAuthException(AccessTokenRequestErrorCode.InvalidRequest, "wrong account_type value.", 400); } AuthRequest authRequest = null; if (accountType == AccountType.UserCenter) { authRequest = new AuthUserCenterRequest(); } else if (accountType == AccountType.ThirdToken) { authRequest = new AuthThirdRequest(); } else { throw new OAuthException(AccessTokenRequestErrorCode.UnsupportedAccountType, "account_type:" + accountType.ToValue(), 400); } authRequest.Parse(request); return(authRequest); }
/// <summary> /// 从HTTP请求中解析凭证的信息 /// </summary> /// <param name="request">Http请求</param> /// <returns>凭证请求对象</returns> public static TokenRequestBase ParseTokenRequest(HttpRequestBase request) { Arguments.NotNull(request, "request"); GrantType grantType; if (!GrantTypeExtend.TryParse(GetString(request, Protocal.GRANT_TYPE), out grantType)) { throw new OAuthException(AccessTokenRequestErrorCode.InvalidRequest, "wrong grant_type, value", 400); } TokenRequestBase tokenRequest = null; switch (grantType) { case GrantType.Password: AccountType accountType; if (!AccountTypeExtend.TryParse(GetString(request, Protocal.ACCOUNT_TYPE), out accountType)) { throw new OAuthException( AccessTokenRequestErrorCode.InvalidRequest, "wrong account_type value", 400); } switch (accountType) { case AccountType.UserCenter: tokenRequest = new TokenPasswordUserCenterRequest(); break; case AccountType.ThirdToken: tokenRequest = new TokenPasswordThirdTokenRequest(); break; default: throw new OAuthException( AccessTokenRequestErrorCode.UnsupportedAccountType, "account_type: " + accountType.ToValue(), 400); } break; case GrantType.ClientCredentials: tokenRequest = new TokenClientCredentialsRequest(); break; case GrantType.RefreshToken: tokenRequest = new TokenRefreshRequest(); break; case GrantType.AuthorizationCode: tokenRequest = new TokenAuthrizationCodeRequest(); break; case GrantType.UserToken: tokenRequest = new TokenUserRequest(); break; default: throw new OAuthException(AccessTokenRequestErrorCode.InvalidRequest, "wrong grant_type, value", 400); } tokenRequest.Parse(request); return(tokenRequest); }