[GH-ISSUE #230] "expires_in" is not getting stored in client.credentials #131

Closed
opened 2026-03-03 16:45:56 +03:00 by kerem · 2 comments
Owner

Originally created by @robertofrontado on GitHub (May 19, 2016).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/230

Hello,

I'm getting some troubles with credential.isTokenExpired() value, since oauth_token_expires_at is always nil

I'm using authorizeWithCallbackURL method,

I believe it should have this

if let expiresIn:String = responseParameters["expires_in"], offset = Double(expiresIn)  {
    self.client.credential.oauth_token_expires_at = NSDate(timeInterval: offset, sinceDate: NSDate())
}

Before the line 83 of the code below

public func authorizeWithCallbackURL(callbackURL: NSURL, scope: String, state: String, params: [String: String] = [String: String](), success: TokenSuccessHandler, failure: ((error: NSError) -> Void)) {

         self.observeCallback { [unowned self] url in
            var responseParameters = [String: String]()
            if let query = url.query {
                responseParameters += query.parametersFromQueryString()
            }
            if let fragment = url.fragment where !fragment.isEmpty {
                responseParameters += fragment.parametersFromQueryString()
            }
            if let accessToken = responseParameters["access_token"] {
                self.client.credential.oauth_token = accessToken.safeStringByRemovingPercentEncoding
                success(credential: self.client.credential, response: nil, parameters: responseParameters)
            }
            else if let code = responseParameters["code"] {
                if !self.allowMissingStateCheck {
                    guard let responseState = responseParameters["state"] else {
                        let errorInfo = [NSLocalizedDescriptionKey: "Missing state"]
                        failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.MissingStateError.rawValue, userInfo: errorInfo))
                        return
                    }
                    if responseState != state {
                        let errorInfo = [NSLocalizedDescriptionKey: "state not equals"]
                        failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.StateNotEqualError.rawValue, userInfo: errorInfo))
                        return
                    }
                }
                self.postOAuthAccessTokenWithRequestTokenByCode(code.safeStringByRemovingPercentEncoding,
                    callbackURL:callbackURL, success: success, failure: failure)
            }
            else if let error = responseParameters["error"], error_description = responseParameters["error_description"] {
                let errorInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString(error, comment: error_description)]
                failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.GeneralError.rawValue, userInfo: errorInfo))
            }
            else {
                let errorInfo = [NSLocalizedDescriptionKey: "No access_token, no code and no error provided by server"]
                failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.ServerError.rawValue, userInfo: errorInfo))
            }
        }


        var queryString = "client_id=\(self.consumer_key)"
        queryString += "&redirect_uri=\(callbackURL.absoluteString)"
        queryString += "&response_type=\(self.response_type)"
        if !scope.isEmpty {
            queryString += "&scope=\(scope)"
        }
        if !state.isEmpty {
            queryString += "&state=\(state)"
        }
        for param in params {
            queryString += "&\(param.0)=\(param.1)"
        }

        var urlString = self.authorize_url
        urlString += (self.authorize_url.has("?") ? "&" : "?")

        if let encodedQuery = queryString.urlQueryEncoded, queryURL = NSURL(string: urlString + encodedQuery) {
            self.authorize_url_handler.handle(queryURL)
        }
        else {
            let errorInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString("Failed to create URL", comment: "\(urlString) or \(queryString) not convertible to URL, please check encoding")]
            failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.EncodingError.rawValue, userInfo: errorInfo))
        }
    }
Originally created by @robertofrontado on GitHub (May 19, 2016). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/230 Hello, I'm getting some troubles with `credential.isTokenExpired()` value, since `oauth_token_expires_at` is always `nil` I'm using authorizeWithCallbackURL method, I believe it should have this ``` swift if let expiresIn:String = responseParameters["expires_in"], offset = Double(expiresIn) { self.client.credential.oauth_token_expires_at = NSDate(timeInterval: offset, sinceDate: NSDate()) } ``` Before the line 83 of the code below ``` swift public func authorizeWithCallbackURL(callbackURL: NSURL, scope: String, state: String, params: [String: String] = [String: String](), success: TokenSuccessHandler, failure: ((error: NSError) -> Void)) { self.observeCallback { [unowned self] url in var responseParameters = [String: String]() if let query = url.query { responseParameters += query.parametersFromQueryString() } if let fragment = url.fragment where !fragment.isEmpty { responseParameters += fragment.parametersFromQueryString() } if let accessToken = responseParameters["access_token"] { self.client.credential.oauth_token = accessToken.safeStringByRemovingPercentEncoding success(credential: self.client.credential, response: nil, parameters: responseParameters) } else if let code = responseParameters["code"] { if !self.allowMissingStateCheck { guard let responseState = responseParameters["state"] else { let errorInfo = [NSLocalizedDescriptionKey: "Missing state"] failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.MissingStateError.rawValue, userInfo: errorInfo)) return } if responseState != state { let errorInfo = [NSLocalizedDescriptionKey: "state not equals"] failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.StateNotEqualError.rawValue, userInfo: errorInfo)) return } } self.postOAuthAccessTokenWithRequestTokenByCode(code.safeStringByRemovingPercentEncoding, callbackURL:callbackURL, success: success, failure: failure) } else if let error = responseParameters["error"], error_description = responseParameters["error_description"] { let errorInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString(error, comment: error_description)] failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.GeneralError.rawValue, userInfo: errorInfo)) } else { let errorInfo = [NSLocalizedDescriptionKey: "No access_token, no code and no error provided by server"] failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.ServerError.rawValue, userInfo: errorInfo)) } } var queryString = "client_id=\(self.consumer_key)" queryString += "&redirect_uri=\(callbackURL.absoluteString)" queryString += "&response_type=\(self.response_type)" if !scope.isEmpty { queryString += "&scope=\(scope)" } if !state.isEmpty { queryString += "&state=\(state)" } for param in params { queryString += "&\(param.0)=\(param.1)" } var urlString = self.authorize_url urlString += (self.authorize_url.has("?") ? "&" : "?") if let encodedQuery = queryString.urlQueryEncoded, queryURL = NSURL(string: urlString + encodedQuery) { self.authorize_url_handler.handle(queryURL) } else { let errorInfo = [NSLocalizedFailureReasonErrorKey: NSLocalizedString("Failed to create URL", comment: "\(urlString) or \(queryString) not convertible to URL, please check encoding")] failure(error: NSError(domain: OAuthSwiftErrorDomain, code: OAuthSwiftErrorCode.EncodingError.rawValue, userInfo: errorInfo)) } } ```
kerem 2026-03-03 16:45:56 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@phimage commented on GitHub (May 25, 2016):

I suppose its in oauth 2 swift file
I see the code to store into requestOAuthAccessTokenWithParameters method. Do you see it?
If you think you are right and code to store misplaced please make a PR

<!-- gh-comment-id:221522260 --> @phimage commented on GitHub (May 25, 2016): I suppose its in oauth 2 swift file I see the code to store into requestOAuthAccessTokenWithParameters method. Do you see it? If you think you are right and code to store misplaced please make a PR
Author
Owner

@phimage commented on GitHub (Jul 7, 2016):

ok I see, in "access_token" there is a bug
requestOAuthAccessTokenWithParameters work only with "code"

<!-- gh-comment-id:230991017 --> @phimage commented on GitHub (Jul 7, 2016): ok I see, in "access_token" there is a bug `requestOAuthAccessTokenWithParameters` work only with "code"
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#131
No description provided.