added iOS source code
[wl-app.git] / iOS / Pods / OAuthSwift / Sources / NSError+OAuthSwift.swift
1 //
2 //  NSError+OAuthSwift.swift
3 //  OAuthSwift
4 //
5 //  Created by Goessler, Florian on 04/04/16.
6 //  Copyright © 2016 Dongri Jin. All rights reserved.
7 //
8
9 import Foundation
10
11 public extension NSError {
12
13     /// Checks the headers contained in the userInfo whether this error was caused by an 
14     /// expired/invalid access token.
15     ///
16     /// Criteria for invalid token error: WWW-Authenticate header contains a field "error" with
17     /// value "invalid_token".
18     ///
19     /// Also implements a special handling for the Facebook API, which indicates invalid tokens in a 
20     /// different manner. See https://developers.facebook.com/docs/graph-api/using-graph-api#errors
21         public var isExpiredToken: Bool {
22         guard self.domain == NSURLErrorDomain else {
23             return false
24         }
25                 if self.code == 401 {
26                         if let reponseHeaders = self.userInfo["Response-Headers"] as? [String: String],
27                                 let authenticateHeader = reponseHeaders["WWW-Authenticate"] ?? reponseHeaders["Www-Authenticate"] {
28                                 let headerDictionary = authenticateHeader.headerDictionary
29                                 if let error = headerDictionary["error"], error == "invalid_token" || error == "\"invalid_token\"" {
30                                         return true
31                                 }
32                         }
33             if let body = self.userInfo["Response-Body"] as? String,
34                 let bodyData = body.data(using: OAuthSwiftDataEncoding),
35                 let json = try? JSONSerialization.jsonObject(with: bodyData, options: []),
36                 let jsonDic = json as? [String: AnyObject] {
37                 if let error = jsonDic["error"] as? String, error == "invalid_token" || error == "\"invalid_token\"" {
38                     return true
39                 }
40                 if let errors = jsonDic["errors"] as? [[String: AnyObject]] {
41                     for error in errors {
42                         if let errorType = error["errorType"] as? String, errorType == "invalid_token" {
43                             return true
44                         }
45                     }
46                 }
47             }
48                 }
49
50         // Detect access token expiration errors from facebook
51         // Docu: https://developers.facebook.com/docs/graph-api/using-graph-api#errors
52         if self.code == 400 {
53             if let urlString = self.userInfo[NSURLErrorFailingURLErrorKey] as? String, urlString.contains("graph.facebook.com") {
54                 if let body = self.userInfo["Response-Body"] as? String,
55                     let bodyData = body.data(using: OAuthSwiftDataEncoding),
56                     let json = try? JSONSerialization.jsonObject(with: bodyData, options: []),
57                     let jsonDic = json as? [String: AnyObject] {
58                     let errorCode = jsonDic["error"]?["code"] as? Int
59                     let errorSubCode = jsonDic["error"]?["error_subcode"] as? Int
60                     if (errorCode == 102 && errorSubCode == nil) || errorSubCode == 463 || errorSubCode == 467 {
61                         return true
62                     }
63                 }
64             }
65         }
66
67                 return false
68         }
69
70 }