Skip to content

Commit

Permalink
🐛 Actually persist token in keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSkwiggs committed Mar 24, 2023
1 parent 6837f3c commit 872f5a1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ Start by importing `GithubKit` and configure it with your app credentials:
```swift
import GithubKit

Github.configure(accessGroup: <keychain identifier>,
clientID: <app client id>,
clientSecret: <app client secret>,
redirectURL: URL(string: <app redirect url>)!)
Github.configure(
clientID: <app client id>,
clientSecret: <app client secret>,
redirectURL: URL(string: <app redirect url>)!
)
```

### Auth
Expand Down
8 changes: 7 additions & 1 deletion Sources/Auth/AuthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public extension View {
@ViewBuilder
func githubAuthSheet(isPresented: Binding<Bool>, _ completion: @escaping Github.Completion) -> some View {
if let github = Github.shared {
self.modifier(GithubAuthView(isPresented: isPresented, github: github, completion))
if let config = github.persistenceManager.readConfiguration() {
self.onAppear {
completion(.success(config))
}
} else {
self.modifier(GithubAuthView(isPresented: isPresented, github: github, completion))
}
} else {
fatalError("Missing call to Github.configure(_:)\n please make sure you set up the library correctly.")
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/Auth/Authview+ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SwiftUI
extension GithubAuthView {
class ViewModel: ObservableObject {

private let github: Github
private let completion: Github.Completion
let webView: WKWebView
let webViewStore: WebViewStore
Expand All @@ -30,6 +31,7 @@ extension GithubAuthView {
private let requestBuilder: Github.Configuration.RequestBuilder

init(github: Github, completion: @escaping Github.Completion) {
self.github = github
self.completion = completion
let webView = WKWebView()
self.webView = webView
Expand Down Expand Up @@ -69,7 +71,9 @@ extension GithubAuthView {
isBusy = true
Task {
do {
completion(.success(try await requestBuilder.getOAuthToken(fromAccessCode: code)))
let token = try await requestBuilder.getOAuthToken(fromAccessCode: code)
github.persistenceManager.persistConfiguration(token)
completion(.success(token))
} catch {
completion(.failure(error))
}
Expand Down
3 changes: 1 addition & 2 deletions Sources/Core/Github.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public struct Github {
static private(set) var shared: Github?

public static func configure(
accessGroup: String,
clientID: String,
clientSecret: String,
baseURL: URL = URL(string: "https://github.com/login/oauth")!,
Expand All @@ -28,7 +27,7 @@ public struct Github {
baseURL: baseURL,
redirectURL: redirectURL
),
persistenceManager: .init(accessGroup: accessGroup)
persistenceManager: .init()
)
}
}
8 changes: 3 additions & 5 deletions Sources/Core/Persistence/PersistenceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class PersistenceManager {

private static let key: String = "config"

init(accessGroup: String) {
let keychain: KeychainSwift = .init()
keychain.accessGroup = accessGroup
self.keychain = keychain
init() {
self.keychain = .init()
self.jsonCoder = .init()
}

Expand All @@ -29,6 +27,6 @@ class PersistenceManager {

func persistConfiguration(_ config: Github.Configuration) {
guard let data = try? jsonCoder.encode(value: config) else { return }
keychain.set(data, forKey: Self.key)
keychain.set(data, forKey: Self.key, withAccess: .accessibleWhenUnlocked)
}
}

0 comments on commit 872f5a1

Please sign in to comment.