[GH-ISSUE #422] Fitbit expired token not handled #271

Closed
opened 2026-03-03 16:47:16 +03:00 by kerem · 6 comments
Owner

Originally created by @hardikdevios on GitHub (Dec 14, 2017).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/422

Description:

Fitbit expired token not handled

if self.domain == NSURLErrorDomain && self.code == 401 {
   if let reponseHeaders = self.userInfo["Response-Headers"] as? [String:String],
   let authenticateHeader = reponseHeaders["WWW-Authenticate"] ??  reponseHeaders["Www-Authenticate"] {
	let headerDictionary = authenticateHeader.headerDictionary
		if let error = headerDictionary["error"], error == "invalid_token" || error == "\"invalid_token\"" {
					return true
                }else if let error = headerDictionary["Bearer realm"], error == "\"api.fitbit.com\"" {
                    return true
                }
}
}

i have to add this in order to parse that properly

}else if let error = headerDictionary["Bearer realm"], error == "\"api.fitbit.com\"" {
                    return true
}

https://dev.fitbit.com/reference/web-api/oauth2/#refresh-token-errors

OAuth Provider (Twitter, Github, ..):

FitBit

OAuth Version:

  • Version 2

OS (Please fill the version) :

  • iOS

Installation method:

  • CocoaPods

Library version:

  • v1.2 (Swift 4.0)

Xcode version:

  • 9.2 (Swift 4.0)
Originally created by @hardikdevios on GitHub (Dec 14, 2017). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/422 ### Description: Fitbit expired token not handled ``` if self.domain == NSURLErrorDomain && self.code == 401 { if let reponseHeaders = self.userInfo["Response-Headers"] as? [String:String], let authenticateHeader = reponseHeaders["WWW-Authenticate"] ?? reponseHeaders["Www-Authenticate"] { let headerDictionary = authenticateHeader.headerDictionary if let error = headerDictionary["error"], error == "invalid_token" || error == "\"invalid_token\"" { return true }else if let error = headerDictionary["Bearer realm"], error == "\"api.fitbit.com\"" { return true } } } ``` i have to add this in order to parse that properly ``` }else if let error = headerDictionary["Bearer realm"], error == "\"api.fitbit.com\"" { return true } ``` https://dev.fitbit.com/reference/web-api/oauth2/#refresh-token-errors ### OAuth Provider (Twitter, Github, ..): **FitBit** ### OAuth Version: - [x] Version 2 ### OS (Please fill the version) : - [x] iOS ### Installation method: - [x] CocoaPods ### Library version: - [x] v1.2 (Swift 4.0) ### Xcode version: - [x] 9.2 (Swift 4.0)
kerem 2026-03-03 16:47:16 +03:00
Author
Owner

@phimage commented on GitHub (Dec 14, 2017):

Could you provide the entire content of header in this case? (you can remove token value etc...)

Maybe there is other interesting http headers . ( headerDictionary["errors"] ?)

<!-- gh-comment-id:351641920 --> @phimage commented on GitHub (Dec 14, 2017): Could you provide the entire content of header in this case? (you can remove token value etc...) Maybe there is other interesting http headers . (` headerDictionary["errors"]` ?)
Author
Owner

@hardikdevios commented on GitHub (Dec 14, 2017):

Error Domain=NSURLErrorDomain Code=401 "" 
UserInfo={Response-Body={"errors":[{"errorType":"invalid_token","message":"Access token invalid: MY Token. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}, 
NSErrorFailingURLKey=https://api.fitbit.com/1/user/-/body/log/weight/date/2017-12-14/1m.json, 

Response-Headers={
    "Content-Length" = 423;
    "Content-Type" = "application/json;charset=utf-8";
    Date = "Thu, 14 Dec 2017 09:47:03 GMT";
    Server = "cloudflare-nginx";
    "Www-Authenticate" = "Bearer realm=\"api.fitbit.com\"";
    "cf-ray" = "3cd03d4c8b11889c-BOM";
    "x-frame-options" = SAMEORIGIN;
}, OAuthSwiftError.response=<NSHTTPURLResponse: 0x1c4822260> { URL: https://api.fitbit.com/1/user/-/body/log/weight/date/2017-12-14/1m.json } { Status Code: 401, Headers {
    "Content-Length" =     (
        423
    );
    "Content-Type" =     (
        "application/json;charset=utf-8"
    );
    Date =     (
        "Thu, 14 Dec 2017 09:47:03 GMT"
    );
    Server =     (
        "cloudflare-nginx"
    );
    "Www-Authenticate" =     (
        "Bearer realm=\"api.fitbit.com\""
    );
    "cf-ray" =     (
        "3cd03d4c8b11889c-BOM"
    );
    "x-frame-options" =     (
        SAMEORIGIN
    );
} }, OAuthSwiftError.response.data=data, NSLocalizedDescription=}

As you can see parsing is failing here for Www-Authenticate because the data which we are looking for invalid-token is not in headerDictionary its actually in Response-Body, so you can handle it properly i just have changed for my quick fix but there is always a room for improvement

<!-- gh-comment-id:351661377 --> @hardikdevios commented on GitHub (Dec 14, 2017): ``` Error Domain=NSURLErrorDomain Code=401 "" UserInfo={Response-Body={"errors":[{"errorType":"invalid_token","message":"Access token invalid: MY Token. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}, NSErrorFailingURLKey=https://api.fitbit.com/1/user/-/body/log/weight/date/2017-12-14/1m.json, Response-Headers={ "Content-Length" = 423; "Content-Type" = "application/json;charset=utf-8"; Date = "Thu, 14 Dec 2017 09:47:03 GMT"; Server = "cloudflare-nginx"; "Www-Authenticate" = "Bearer realm=\"api.fitbit.com\""; "cf-ray" = "3cd03d4c8b11889c-BOM"; "x-frame-options" = SAMEORIGIN; }, OAuthSwiftError.response=<NSHTTPURLResponse: 0x1c4822260> { URL: https://api.fitbit.com/1/user/-/body/log/weight/date/2017-12-14/1m.json } { Status Code: 401, Headers { "Content-Length" = ( 423 ); "Content-Type" = ( "application/json;charset=utf-8" ); Date = ( "Thu, 14 Dec 2017 09:47:03 GMT" ); Server = ( "cloudflare-nginx" ); "Www-Authenticate" = ( "Bearer realm=\"api.fitbit.com\"" ); "cf-ray" = ( "3cd03d4c8b11889c-BOM" ); "x-frame-options" = ( SAMEORIGIN ); } }, OAuthSwiftError.response.data=data, NSLocalizedDescription=} ``` As you can see parsing is failing here for `Www-Authenticate` because the data which we are looking for invalid-token is not in `headerDictionary` its actually in `Response-Body`, so you can handle it properly i just have changed for my quick fix but there is always a room for improvement
Author
Owner

@phimage commented on GitHub (Dec 14, 2017):

Yes that's why I ask :)
Like Facebook special case, we could parse here "Response-Body" and check here errorType == "invalid_token"

We could limit to fitbit like you do with headerDictionary["Bearer realm"], error == "\"api.fitbit.com\"", to limit potential issues

You could do a PR if you want

<!-- gh-comment-id:351665440 --> @phimage commented on GitHub (Dec 14, 2017): Yes that's why I ask :) Like Facebook special case, we could parse here "Response-Body" and check here errorType == "invalid_token" We could limit to fitbit like you do with `headerDictionary["Bearer realm"], error == "\"api.fitbit.com\""`, to limit potential issues You could do a PR if you want
Author
Owner

@hardikdevios commented on GitHub (Dec 14, 2017):

Sure will do that on weekend, No time now ;)

<!-- gh-comment-id:351667802 --> @hardikdevios commented on GitHub (Dec 14, 2017): Sure will do that on weekend, No time now ;)
Author
Owner

@phimage commented on GitHub (Mar 4, 2018):

No PR so I make a branch fix/fitbiterror
with the modified version of NSError extension

https://github.com/OAuthSwift/OAuthSwift/blob/fix/fitbiterror/Sources/NSError%2BOAuthSwift.swift

So @hardikdevios you can test it

<!-- gh-comment-id:370220038 --> @phimage commented on GitHub (Mar 4, 2018): No PR so I make a branch fix/fitbiterror with the modified version of NSError extension https://github.com/OAuthSwift/OAuthSwift/blob/fix/fitbiterror/Sources/NSError%2BOAuthSwift.swift So @hardikdevios you can test it
Author
Owner

@phimage commented on GitHub (Jun 25, 2018):

I merge branch 4a95cd7ab7

<!-- gh-comment-id:399886954 --> @phimage commented on GitHub (Jun 25, 2018): I merge branch 4a95cd7ab7c6050601ae6d4be02b48b916c3c45f
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#271
No description provided.