gpt4 book ai didi

android - 针对身份服务器进行身份验证时 AppAuth oidc 无效状态错误

转载 作者:行者123 更新时间:2023-11-29 01:12:24 26 4
gpt4 key购买 nike

我已经为一个项目配置了一个身份服务器 3 作为 IdP,我们有 3 个客户端:MVC web、IOS 和 Android。对于 MVC 应用程序,一切都很好,使用混合流。

对于 IOS 和 Android,使用 native oidc 客户端(AppAuth IOSAppAuth android)不起作用,即使我将流程配置为带有 PKCE 的混合。

现在,当我尝试使用 Xamarin 在 Android 上制作 POC 并使用 IdentityModel.oidcClient 时,一切都按预期工作,获得访问权限、刷新和 ID token 。在 IOS 和 Android 上使用 AppAuth 时出现以下错误:

{"type":0,"code":9,"errorDescription":"Response state param did not match request state"}

知道缺少什么吗?

我怀疑这两个本地 oidc 客户端没有请求客户端的共享 key ,因此流已损坏。

最佳答案

移动应用程序和身份服务器中的数据应该相同,

在服务器上:

new Client
{
ClientId = "myClientId",
ClientName = "myClientName",
AllowedGrantTypes = GrantTypes.CodeAndClientCredentials,
RequireConsent = false,

ClientSecrets =
{
new Secret("myClientSecret".Sha256())
},

RedirectUris = { "myRedirectUri://callback" },

AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
IdentityServerConstants.StandardScopes.Phone,
},

AllowOfflineAccess = true
}

在android中点击登录:

    AuthManager authManager = AuthManager.getInstance(this);
AuthorizationService authService = authManager.getAuthService();
Auth auth = authManager.getAuth();

AuthorizationRequest authRequest = new AuthorizationRequest
.Builder(
authManager.getAuthConfig(),
auth.getClientId(),
auth.getResponseType(),
Uri.parse(auth.getRedirectUri()))
.setScope(auth.getScope())
.build();

Intent authIntent = new Intent(this, LoginAuthActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, authRequest.hashCode(), authIntent, 0);

authService.performAuthorizationRequest(
authRequest,
pendingIntent);

请求 token :

    final AuthorizationResponse resp = AuthorizationResponse.fromIntent(getIntent());
AuthorizationException ex = AuthorizationException.fromIntent(getIntent());

final AuthManager authManager = AuthManager.getInstance(this);
authManager.setAuthState(resp,ex);

if (resp != null) {

ClientSecretPost clientSecretPost = new ClientSecretPost(authManager.getAuth().getClientSecret());
TokenRequest tokenRequest = new TokenRequest
.Builder(authManager.getAuthConfig(), authManager.getAuth().getClientId())
.setAuthorizationCode(resp.authorizationCode)
.setRedirectUri(Uri.parse(authManager.getAuth().getRedirectUri()))
.build();

mAuthService = authManager.getAuthService();

mAuthService.performTokenRequest(tokenRequest, clientSecretPost, new AuthorizationService.TokenResponseCallback() {
@Override public void onTokenRequestCompleted(@Nullable TokenResponse response, @Nullable AuthorizationException ex) {
if(ex == null) {
authManager.updateAuthState(response,ex);
MyApp.Token = authManager.getAuthState().getIdToken();
startService(new Intent(LoginAuthActivity.this, TokenService.class));
Intent mainIntent = new Intent(LoginAuthActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
else{
Intent loginIntent = new Intent(LoginAuthActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}
}
});

// authorization completed
} else {
// authorization failed, check ex for more details
Intent loginIntent = new Intent(LoginAuthActivity.this, LoginActivity.class);
startActivity(loginIntent);
finish();
}

AuthManager 类:

public class AuthManager {
private static AuthManager instance;
private AuthState mAuthState;
private Auth mAuth;
private AuthorizationServiceConfiguration mAuthConfig;
private SharedPreferencesRepository mSharedPrefRep;
private AuthorizationService mAuthService;

public static AuthManager getInstance(Context context) {
if (instance == null) {
instance = new AuthManager(context);
}
return instance;
}

private AuthManager(Context context){
mSharedPrefRep = new SharedPreferencesRepository(context);
setAuthData();
mAuthConfig = new AuthorizationServiceConfiguration(
Uri.parse(mAuth.getAuthorizationEndpointUri()),
Uri.parse(mAuth.getTokenEndpointUri()),
null);
mAuthState = mSharedPrefRep.getAuthState();

mAuthService = new AuthorizationService(context);
}



public AuthorizationServiceConfiguration getAuthConfig() {
return mAuthConfig;
}

public Auth getAuth() {
if(mAuth == null){
setAuthData();
}

return mAuth;
}

public AuthState getAuthState(){
return mAuthState;
}

public void updateAuthState(TokenResponse response, AuthorizationException ex){
mAuthState.update(response,ex);
mSharedPrefRep.saveAuthState(mAuthState);
}

public void setAuthState(AuthorizationResponse response, AuthorizationException ex){
if(mAuthState == null)
mAuthState = new AuthState(response,ex);

mSharedPrefRep.saveAuthState(mAuthState);
}

public AuthorizationService getAuthService(){
return mAuthService;
}

private void setAuthData(){
mAuth = new Auth();
mAuth.setClientId(BuildConfig.CLIENT_ID);
mAuth.setAuthorizationEndpointUri(BuildConfig.AUTHORIZSTION_END_POINT_URI);
mAuth.setClientSecret(BuildConfig.CLIENT_SECRET);
mAuth.setRedirectUri(BuildConfig.REDIRECT_URI);
mAuth.setScope(BuildConfig.SCOPE);
mAuth.setTokenEndpointUri(BuildConfig.TOKEN_END_POINT_URI);
mAuth.setResponseType(BuildConfig.RESPONSE_TYPE);
}
}

此处的服务将请求刷新 token 。

我已经使用带有 AppAuth-Android 的 Identity Server 4 制作了一个示例,您可以查看它 here

关于android - 针对身份服务器进行身份验证时 AppAuth oidc 无效状态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41973244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com