From 11394a789f134582833cb742f9e88f8df68a9680 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Wed, 11 Mar 2020 02:07:35 +0100 Subject: [PATCH 1/3] Minimal refresh token support --- .../Imperial/Helpers/Sessions+Imperial.swift | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index 9f67a42..1a5de25 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -12,6 +12,16 @@ extension Request { public func accessToken()throws -> String { return try self.session().accessToken() } + + /// Gets the refresh token from the current session. + /// + /// - Returns: The refresh token in the current session. + /// - Throws: + /// - `Abort.unauthorized` if no refresh token exists. + /// - `SessionsError.notConfigured` if session middlware is not configured yet. + public func refreshToken()throws -> String { + return try self.session().refreshToken() + } } extension Session { @@ -19,12 +29,13 @@ extension Session { /// Keys used to store and retrieve items from the session enum Keys { static let token = "access_token" + static let refresh = "refresh_token" } /// Gets the access token from the session. /// /// - Returns: The access token stored with the `access_token` key. - /// - Throws: `Abort.unauthorized` if no access token exists.m + /// - Throws: `Abort.unauthorized` if no access token exists. public func accessToken()throws -> String { guard let token = self[Keys.token] else { throw Abort(.unauthorized, reason: "User currently not authenticated") @@ -38,7 +49,25 @@ extension Session { public func setAccessToken(_ token: String) { self[Keys.token] = token } - + + /// Gets the refresh token from the session. + /// + /// - Returns: The refresh token stored with the `refresh_token` key. + /// - Throws: `Abort.unauthorized` if no refresh token exists. + public func refreshToken()throws -> String { + guard let token = self[Keys.refresh] else { + throw Abort(.unauthorized, reason: "User currently not authenticated") + } + return token + } + + /// Sets the refresh token on the session. + /// + /// - Parameter token: the refresh token to store on the session + public func setRefreshToken(_ token: String) { + self[Keys.refresh] = token + } + /// Gets an object stored in a session with JSON as a given type. /// /// - Parameters: From 135e33dc59a3a56f745a1748b2ca093036987ba2 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Sun, 15 Mar 2020 00:30:11 +0100 Subject: [PATCH 2/3] More accurate error reporting for missing refresh token --- Sources/Imperial/Helpers/Sessions+Imperial.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index 1a5de25..e9c7eaf 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -56,7 +56,14 @@ extension Session { /// - Throws: `Abort.unauthorized` if no refresh token exists. public func refreshToken()throws -> String { guard let token = self[Keys.refresh] else { - throw Abort(.unauthorized, reason: "User currently not authenticated") + if self[Keys.token] == nil { + throw Abort(.unauthorized, reason: "User currently not authenticated") + } else { + let oauth_data = self["access_token_service"]?.data(using: .utf8) ?? Data() + let oauth = try? JSONSerialization.jsonObject(with: oauth_data, options: []) + let oauth_name = (oauth as? NSDictionary)?["name"] ?? "???" + throw Abort(.methodNotAllowed, reason: "OAuth provider '\(oauth_name)' uses no refresh tokens") + } } return token } From 6c496cc51bcc174033c531f28bb4cb35097eecd2 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Wed, 1 Apr 2020 22:01:35 +0200 Subject: [PATCH 3/3] Eliminate snake_case in variable names --- Sources/Imperial/Helpers/Sessions+Imperial.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/Imperial/Helpers/Sessions+Imperial.swift b/Sources/Imperial/Helpers/Sessions+Imperial.swift index e9c7eaf..718e5e9 100644 --- a/Sources/Imperial/Helpers/Sessions+Imperial.swift +++ b/Sources/Imperial/Helpers/Sessions+Imperial.swift @@ -59,10 +59,10 @@ extension Session { if self[Keys.token] == nil { throw Abort(.unauthorized, reason: "User currently not authenticated") } else { - let oauth_data = self["access_token_service"]?.data(using: .utf8) ?? Data() - let oauth = try? JSONSerialization.jsonObject(with: oauth_data, options: []) - let oauth_name = (oauth as? NSDictionary)?["name"] ?? "???" - throw Abort(.methodNotAllowed, reason: "OAuth provider '\(oauth_name)' uses no refresh tokens") + let oauthData = self["access_token_service"]?.data(using: .utf8) ?? Data() + let oauth = try? JSONSerialization.jsonObject(with: oauthData, options: []) + let oauthName = (oauth as? NSDictionary)?["name"] ?? "???" + throw Abort(.methodNotAllowed, reason: "OAuth provider '\(oauthName)' uses no refresh tokens") } } return token