Skip to content

Commit 550e863

Browse files
committed
Provide a way to store the login state when logging in manually
1 parent 3c3b35a commit 550e863

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Sources/Core/Authentication/AccessToken.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ public class AccessToken: Decodable, CustomStringConvertible {
7373
}
7474
return accessToken
7575
}
76+
77+
init(grantType: OAuthenticationGrantType, accessToken: String, refreshToken: String, expireDate: Date, host: String? = nil) {
78+
self.grantType = grantType
79+
self.accessToken = accessToken
80+
self.refreshToken = refreshToken
81+
self.expireDate = expireDate
82+
self.host = host ?? ""
83+
}
7684

7785
init(host: String) {
7886
self.host = host

Sources/Core/Authentication/AuthenticationProvider.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,31 @@ class AuthenticationProvider {
174174
self.isAuthenticating = false
175175
}
176176
}
177+
178+
///
179+
/// Handle a manual OAuth authentication call when using differently named properties
180+
///
181+
/// - Parameters:
182+
/// - grantType: `OAuthenticationGrantType`
183+
/// - accessToken: `String`
184+
/// - refreshToken: `OAuthenticationGrantType`
185+
/// - expiresIn: `Int`
186+
/// - host: `String?`
187+
///
188+
/// - Returns: `Void`
189+
///
190+
func handleManualOAuthRequest(grantType: OAuthenticationGrantType,
191+
accessToken: String,
192+
refreshToken: String,
193+
expireDate: Date,
194+
host: String? = nil) {
195+
let oauthHost = (host ?? client.config.host) ?? ""
196+
let accessTokenObject = AccessToken(grantType: grantType, accessToken: accessToken, refreshToken: refreshToken, expireDate: expireDate, host: oauthHost)
197+
accessTokenObject.store()
198+
199+
client.logger?.debug("Store access-token: \(optionalDescription(accessToken))")
200+
client.authorizationGrantTypeSubject.onNext(grantType)
201+
}
177202

178203
// MARK: - Recover
179204
// --------------------------------------------------------

Sources/Core/Base/Client.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,24 @@ open class Client: ReactiveCompatible {
219219
]
220220
return authProvider.sendOAuthRequest(grantType: .password, parameters: parameters)
221221
}
222+
223+
/// Handle the result of a manual login call
224+
///
225+
/// - Parameters:
226+
/// - grantType: `OAuthenticationGrantType`
227+
/// - accessToken: `String`
228+
/// - refreshToken: `OAuthenticationGrantType`
229+
/// - expiresIn: `Int`
230+
/// - host: `String?`
231+
///
232+
/// - Returns: `Void`
233+
public func loggedIn(grantType: OAuthenticationGrantType,
234+
accessToken: String,
235+
refreshToken: String,
236+
expireDate: Date,
237+
host: String? = nil) {
238+
authProvider.handleManualOAuthRequest(grantType: grantType, accessToken: accessToken, refreshToken: refreshToken, expireDate: expireDate, host: host)
239+
}
222240

223241
public func clearAccessToken(forHost host: String? = nil) {
224242
authorizationGrantTypeSubject.onNext(nil)

0 commit comments

Comments
 (0)