{"action":"created","comment":{"url":"https://api.github.com/repos/edx/edx-app-ios/pulls/comments/64957377","id":64957377,"diff_hunk":"@@ -0,0 +1,80 @@\n+//\n+//  NetworkManager+Authenticators.swift\n+//  edX\n+//\n+//  Created by Christopher Lee on 5/13/16.\n+//  Copyright © 2016 edX. All rights reserved.\n+//\n+\n+import Foundation\n+\n+import edXCore\n+\n+extension NetworkManager {\n+    @objc public func addRefreshTokenAuthenticator(router:OEXRouter, session:OEXSession, clientId:String) {\n+        let invalidAccessAuthenticator = {[weak router] response, data in\n+            NetworkManager.invalidAccessAuthenticator(router, session: session, clientId:clientId, response: response, data: data)\n+        }\n+        setupAuthenticator(invalidAccessAuthenticator)\n+    }\n+    \n+    /** Checks if the response's status code is 401. Then checks the error\n+     message for an expired access token. If so, a new network request to\n+     refresh the access token is made and this new access token is saved.\n+     */\n+    public static func invalidAccessAuthenticator(router: OEXRouter?, session:OEXSession, clientId:String, response: NSHTTPURLResponse?, data: NSData?) -> AuthenticationAction {\n+        if let data = data,\n+            response = response,\n+            raw : AnyObject = try? NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())\n+        {\n+            let json = JSON(raw)\n+            \n+            guard let statusCode = OEXHTTPStatusCode(rawValue: response.statusCode),\n+                error = NSError(json: json, code: response.statusCode)\n+                where statusCode == .Code401Unauthorised else\n+            {\n+                return AuthenticationAction.Proceed\n+            }\n+            \n+            guard let refreshToken = session.token?.refreshToken else {\n+                return logout(router)\n+            }\n+            \n+            if error.isAPIError(.OAuth2Expired) {\n+                return refreshAccessToken(clientId, refreshToken: refreshToken, session: session)\n+            }\n+            if error.isAPIError(.OAuth2Nonexistent) {\n+                return logout(router)\n+            }\n+        }\n+        return logout(router)\n+    }\n+}\n+\n+private func logout(router:OEXRouter?) -> AuthenticationAction {\n+    dispatch_async(dispatch_get_main_queue()) {\n+        router?.logout()\n+    }\n+    return AuthenticationAction.Proceed\n+}\n+\n+/** Creates a networkRequest to refresh the access_token. If successful, the\n+ new access token is saved and a successful AuthenticationAction is returned.\n+ */\n+private func refreshAccessToken(clientId:String, refreshToken:String, session: OEXSession) -> AuthenticationAction {\n+    return AuthenticationAction.Authenticate({ (networkManager, completion) in\n+        let networkRequest = LoginAPI.refreshAccessToken(\n+            refreshToken,\n+            clientId: clientId,\n+            grantType: \"refresh_token\"\n+        )\n+        networkManager.taskForRequest(networkRequest) {result in\n+            if let newAccessToken = result.data {\n+                session.saveAccessToken(newAccessToken, userDetails: session.currentUser!)","path":"Source/NetworkManager+Authenticators.swift","position":73,"original_position":73,"commit_id":"6ac366ccaa62f2ccf390643bf08c62e4c7f97636","original_commit_id":"6ac366ccaa62f2ccf390643bf08c62e4c7f97636","user":{"login":"aleffert","id":114396,"avatar_url":"https://avatars.githubusercontent.com/u/114396?v=3","gravatar_id":"","url":"https://api.github.com/users/aleffert","html_url":"https://github.com/aleffert","followers_url":"https://api.github.com/users/aleffert/followers","following_url":"https://api.github.com/users/aleffert/following{/other_user}","gists_url":"https://api.github.com/users/aleffert/gists{/gist_id}","starred_url":"https://api.github.com/users/aleffert/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aleffert/subscriptions","organizations_url":"https://api.github.com/users/aleffert/orgs","repos_url":"https://api.github.com/users/aleffert/repos","events_url":"https://api.github.com/users/aleffert/events{/privacy}","received_events_url":"https://api.github.com/users/aleffert/received_events","type":"User","site_admin":false},"body":"We should be careful around ``session.currentUser!`` better to do this by adding a method inside ``OEXSession`` that understands the token got recycled instead of having to use ``!`` - I could imagine some sort of race condition where this is going on and then the user goes to logout and it would crash.","created_at":"2016-05-27T19:41:55Z","updated_at":"2016-05-27T19:41:55Z","html_url":"https://github.com/edx/edx-app-ios/pull/732#discussion_r64957377","pull_request_url":"https://api.github.com/repos/edx/edx-app-ios/pulls/732","_links":{"self":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/comments/64957377"},"html":{"href":"https://github.com/edx/edx-app-ios/pull/732#discussion_r64957377"},"pull_request":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/732"}}},"pull_request":{"url":"https://api.github.com/repos/edx/edx-app-ios/pulls/732","id":70378340,"html_url":"https://github.com/edx/edx-app-ios/pull/732","diff_url":"https://github.com/edx/edx-app-ios/pull/732.diff","patch_url":"https://github.com/edx/edx-app-ios/pull/732.patch","issue_url":"https://api.github.com/repos/edx/edx-app-ios/issues/732","number":732,"state":"open","locked":false,"title":"MA-1772 Refresh access token on 401","user":{"login":"BenjiLee","id":7101723,"avatar_url":"https://avatars.githubusercontent.com/u/7101723?v=3","gravatar_id":"","url":"https://api.github.com/users/BenjiLee","html_url":"https://github.com/BenjiLee","followers_url":"https://api.github.com/users/BenjiLee/followers","following_url":"https://api.github.com/users/BenjiLee/following{/other_user}","gists_url":"https://api.github.com/users/BenjiLee/gists{/gist_id}","starred_url":"https://api.github.com/users/BenjiLee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BenjiLee/subscriptions","organizations_url":"https://api.github.com/users/BenjiLee/orgs","repos_url":"https://api.github.com/users/BenjiLee/repos","events_url":"https://api.github.com/users/BenjiLee/events{/privacy}","received_events_url":"https://api.github.com/users/BenjiLee/received_events","type":"User","site_admin":false},"body":"Addresses https://openedx.atlassian.net/browse/MA-1772\r\n\r\n@mikekatz @aleffert Pleez Review. Tests will come later, I've had enough iOS for the time being. I am going to reward my brain by reviewing some android. ","created_at":"2016-05-17T15:30:58Z","updated_at":"2016-05-27T19:41:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6bec572dc07dd20f014279b9295aabfb01e8f640","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/edx-app-ios/pulls/732/commits","review_comments_url":"https://api.github.com/repos/edx/edx-app-ios/pulls/732/comments","review_comment_url":"https://api.github.com/repos/edx/edx-app-ios/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/edx-app-ios/issues/732/comments","statuses_url":"https://api.github.com/repos/edx/edx-app-ios/statuses/6ac366ccaa62f2ccf390643bf08c62e4c7f97636","head":{"label":"edx:clee/refresh","ref":"clee/refresh","sha":"6ac366ccaa62f2ccf390643bf08c62e4c7f97636","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":29251733,"name":"edx-app-ios","full_name":"edx/edx-app-ios","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-app-ios","description":"","fork":false,"url":"https://api.github.com/repos/edx/edx-app-ios","forks_url":"https://api.github.com/repos/edx/edx-app-ios/forks","keys_url":"https://api.github.com/repos/edx/edx-app-ios/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-app-ios/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-app-ios/teams","hooks_url":"https://api.github.com/repos/edx/edx-app-ios/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-app-ios/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-app-ios/events","assignees_url":"https://api.github.com/repos/edx/edx-app-ios/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-app-ios/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-app-ios/tags","blobs_url":"https://api.github.com/repos/edx/edx-app-ios/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-app-ios/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-app-ios/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-app-ios/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-app-ios/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-app-ios/languages","stargazers_url":"https://api.github.com/repos/edx/edx-app-ios/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-app-ios/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-app-ios/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-app-ios/subscription","commits_url":"https://api.github.com/repos/edx/edx-app-ios/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-app-ios/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-app-ios/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-app-ios/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-app-ios/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-app-ios/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-app-ios/merges","archive_url":"https://api.github.com/repos/edx/edx-app-ios/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-app-ios/downloads","issues_url":"https://api.github.com/repos/edx/edx-app-ios/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-app-ios/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-app-ios/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-app-ios/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-app-ios/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-app-ios/releases{/id}","deployments_url":"https://api.github.com/repos/edx/edx-app-ios/deployments","created_at":"2015-01-14T15:49:29Z","updated_at":"2016-05-20T11:32:23Z","pushed_at":"2016-05-27T18:33:17Z","git_url":"git://github.com/edx/edx-app-ios.git","ssh_url":"git@github.com:edx/edx-app-ios.git","clone_url":"https://github.com/edx/edx-app-ios.git","svn_url":"https://github.com/edx/edx-app-ios","homepage":null,"size":334726,"stargazers_count":61,"watchers_count":61,"language":"Swift","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":67,"mirror_url":null,"open_issues_count":8,"forks":67,"open_issues":8,"watchers":61,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"7ace71673d9e787312427c7294f3789cb56f8faf","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":29251733,"name":"edx-app-ios","full_name":"edx/edx-app-ios","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/edx-app-ios","description":"","fork":false,"url":"https://api.github.com/repos/edx/edx-app-ios","forks_url":"https://api.github.com/repos/edx/edx-app-ios/forks","keys_url":"https://api.github.com/repos/edx/edx-app-ios/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/edx-app-ios/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/edx-app-ios/teams","hooks_url":"https://api.github.com/repos/edx/edx-app-ios/hooks","issue_events_url":"https://api.github.com/repos/edx/edx-app-ios/issues/events{/number}","events_url":"https://api.github.com/repos/edx/edx-app-ios/events","assignees_url":"https://api.github.com/repos/edx/edx-app-ios/assignees{/user}","branches_url":"https://api.github.com/repos/edx/edx-app-ios/branches{/branch}","tags_url":"https://api.github.com/repos/edx/edx-app-ios/tags","blobs_url":"https://api.github.com/repos/edx/edx-app-ios/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/edx-app-ios/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/edx-app-ios/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/edx-app-ios/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/edx-app-ios/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/edx-app-ios/languages","stargazers_url":"https://api.github.com/repos/edx/edx-app-ios/stargazers","contributors_url":"https://api.github.com/repos/edx/edx-app-ios/contributors","subscribers_url":"https://api.github.com/repos/edx/edx-app-ios/subscribers","subscription_url":"https://api.github.com/repos/edx/edx-app-ios/subscription","commits_url":"https://api.github.com/repos/edx/edx-app-ios/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/edx-app-ios/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/edx-app-ios/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/edx-app-ios/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/edx-app-ios/contents/{+path}","compare_url":"https://api.github.com/repos/edx/edx-app-ios/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/edx-app-ios/merges","archive_url":"https://api.github.com/repos/edx/edx-app-ios/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/edx-app-ios/downloads","issues_url":"https://api.github.com/repos/edx/edx-app-ios/issues{/number}","pulls_url":"https://api.github.com/repos/edx/edx-app-ios/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/edx-app-ios/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/edx-app-ios/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/edx-app-ios/labels{/name}","releases_url":"https://api.github.com/repos/edx/edx-app-ios/releases{/id}","deployments_url":"https://api.github.com/repos/edx/edx-app-ios/deployments","created_at":"2015-01-14T15:49:29Z","updated_at":"2016-05-20T11:32:23Z","pushed_at":"2016-05-27T18:33:17Z","git_url":"git://github.com/edx/edx-app-ios.git","ssh_url":"git@github.com:edx/edx-app-ios.git","clone_url":"https://github.com/edx/edx-app-ios.git","svn_url":"https://github.com/edx/edx-app-ios","homepage":null,"size":334726,"stargazers_count":61,"watchers_count":61,"language":"Swift","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":67,"mirror_url":null,"open_issues_count":8,"forks":67,"open_issues":8,"watchers":61,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/732"},"html":{"href":"https://github.com/edx/edx-app-ios/pull/732"},"issue":{"href":"https://api.github.com/repos/edx/edx-app-ios/issues/732"},"comments":{"href":"https://api.github.com/repos/edx/edx-app-ios/issues/732/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/732/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/edx-app-ios/pulls/732/commits"},"statuses":{"href":"https://api.github.com/repos/edx/edx-app-ios/statuses/6ac366ccaa62f2ccf390643bf08c62e4c7f97636"}}}}
{"actor":{"display_login":"aleffert"}}