AppAuth iOS。用户在我自己的OAuth2服务器中注册帐户后如何保存令牌信息

问题描述 投票:12回答:4

我有一个iOS应用程序和一个用Spring编写的后端,并实现了OAuth2机制。

我的后端有一个signup端点,它接受一些用户数据并返回OAuth2响应,例如

{
    "access_token":"9154140d-b621-4391-9fdd-fbba9e5e4188",
    "token_type":"bearer",
    "refresh_token":"dd612036-7cc6-4858-a30f-c548fc2a823c",
    "expires_in":89,
    "scope":"write"
}

之后我想在我的钥匙串中保存OIDAuthState,所以我做了以下事情:

func saveTokenResponse(responseDict: [String: NSCopying & NSObjectProtocol]) {
        let redirectURI = URL(string: kRedirectURI)
        let configuration = OIDServiceConfiguration(authorizationEndpoint: URL(string: kAuthorizationEndpoint)!, tokenEndpoint: URL(string: kTokenEndpoint)!)
        let request = OIDAuthorizationRequest(configuration: configuration, clientId: self.kClientID, scopes: ["write"], redirectURL: redirectURI!, responseType: OIDResponseTypeCode, additionalParameters: nil)

        let accessToken = responseDict["access_token"] ?? nil
        let tokenType = responseDict["token_type"] ?? nil
        let refreshToken = responseDict["refresh_token"] ?? nil
        let expiresIn = responseDict["expires_in"] ?? nil
        let scope = responseDict["scope"] ?? nil

        let response = OIDAuthorizationResponse(request: request, parameters: [
            "access_token": accessToken!,
            "token_type": tokenType!,
            "refresh_token": refreshToken!,
            "expires_in": expiresIn!,
            "scope": scope!
            ]
        )
        let authState = OIDAuthState(authorizationResponse: response) //here authState.refreshToken is nil, so it won't be serialized.

        updateAuthState(authState: authState) // it just saves it in keychain
    }

一切都运作良好,但令牌到期时我遇到了问题。该应用程序调用后端,AppAuth无法刷新令牌:

OIDAuthState does not have refresh token

我可以看到,OIDAuthState对象中不存在刷新令牌。我从OIDAuthState检查了OIDAuthorizationResponse的初始化,发现在这种情况下没有分配令牌。

任何人都可以帮助我从我的后端收到的OAuth响应中保存OIDAuthState吗?或者我是以错误的方式做某事?

谢谢,

奥斯曼

ios oauth-2.0 appauth
4个回答
5
投票

如果您的令牌过期,那么您可以像所有应用程序(银行系统,亚马逊,Paytm和Facebook)一样移动登录屏幕(呼叫退出功能)。因此,用户在使用您的凭据登录时,可以获得您想要的相同响应并需要刷新令牌。您已在登录Web服务中获得samilar信息,如下面的响应。我希望它的工作正常

    let accessToken = responseDict["access_token"] ?? nil
    let tokenType = responseDict["token_type"] ?? nil
    let refreshToken = responseDict["refresh_token"] ?? nil
    let expiresIn = responseDict["expires_in"] ?? nil
    let scope = responseDict["scope"] ?? nil

    let response = OIDAuthorizationResponse(request: request, parameters: [
        "access_token": accessToken!,
        "token_type": tokenType!,
        "refresh_token": refreshToken!,
        "expires_in": expiresIn!,
        "scope": scope!]
       )

2
投票

我不知道你在做什么,但我建议你使用一种广泛使用的不同机制。

每当用户使用Google登录时,Google都会发送临时访问令牌,该令牌会在短时间内过期。

  1. 您登录时获取的访问令牌应该发送到您的服务器,只是为了查看并检查用户是否真实,并且所有他的个人详细信息也可以随之获取(通过GoogleAPI调用)。
  2. 用户通过Google登录后立即将此访问令牌发送到您的服务器。在服务器端自动完成此令牌。
  3. 在对访问令牌进行身份验证后,将您自己的唯一令牌从服务器(具有到期时间)发送给用户。
  4. 用户使用您提供的此唯一令牌继续使用应用。

0
投票

let redirectURI = URL(string: kRedirectURI)字符串更改为小写,oOuth for ios是挑剔的。也可以尝试使用Okta作为中间人服务,插入oAuth。


0
投票

我使用AppAuth for iOS遇到了同样的问题(refreshToken为null)。通过在我的示波器中添加“offline_access”,我能够让它工作。

let request = OIDAuthorizationRequest(configuration: configuration!, clientId: self.kClientID, scopes: [OIDScopeOpenID, OIDScopeProfile, "api", "offline_access"], redirectURL: redirectURI! as URL, responseType: OIDResponseTypeCode, additionalParameters: nil)
© www.soinside.com 2019 - 2024. All rights reserved.