Skip to content

Commit

Permalink
moved tokens from prefs to keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
twocanoes committed Jun 4, 2022
1 parent d8ace70 commit a2ebf9f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 19 deletions.
28 changes: 24 additions & 4 deletions XCreds/MainController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ class MainController: NSObject {
//now we set the password.

DispatchQueue.main.async {
if let userInfo = notification.userInfo, let cloudPassword = userInfo["password"] as? String {
let keychainUtil = KeychainUtil()

guard let userInfo = notification.userInfo else {
return
}

if let accessToken = userInfo[PrefKeys.accessToken.rawValue] as? String {
let _ = keychainUtil.setPassword(PrefKeys.accessToken.rawValue, pass: accessToken)
}

if let idToken = userInfo[PrefKeys.idToken.rawValue] as? String {
let _ = keychainUtil.setPassword(PrefKeys.idToken.rawValue, pass: idToken)
}

if let refreshToken = userInfo[PrefKeys.refreshToken.rawValue] as? String {
let _ = keychainUtil.setPassword(PrefKeys.refreshToken.rawValue, pass: refreshToken)
}



if let cloudPassword = userInfo["password"] as? String {
let localPassword = self.localPassword()

if let localPassword = localPassword {
let verifyOIDPassword = VerifyOIDCPassword.init(windowNibName: NSNib.Name("VerifyOIDCPassword"))
let verifyOIDPassword = VerifyOIDCPasswordWindowController.init(windowNibName: NSNib.Name("VerifyOIDCPassword"))
NSApp.activate(ignoringOtherApps: true)

while true {
Expand Down Expand Up @@ -59,7 +79,7 @@ class MainController: NSObject {
func localPassword() -> String? {
let keychainUtil = KeychainUtil()

let password = try? keychainUtil.findPassword("xcreds")
let password = try? keychainUtil.findPassword("local password")

if let password = password {
if PasswordUtils.verifyCurrentUserPassword(password: password) == true {
Expand All @@ -83,7 +103,7 @@ class MainController: NSObject {
let isPasswordValid = PasswordUtils.verifyCurrentUserPassword(password:localPassword )
if isPasswordValid==true {
passwordWindowController.window?.close()
let err = keychainUtil.setPassword("xcreds", pass: localPassword)
let err = keychainUtil.setPassword("local password", pass: localPassword)
if err != OSStatus(errSecSuccess) {
return nil
}
Expand Down
24 changes: 18 additions & 6 deletions XCreds/TokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,16 @@ class TokenManager {

var req = URLRequest(url: url)

let refreshToken = defaults.string(forKey: PrefKeys.refreshToken.rawValue) ?? ""
let clientID = defaults.string(forKey: PrefKeys.clientID.rawValue) ?? ""
let keychainUtil = KeychainUtil()

var parameters = "grant_type=refresh_token&refresh_token=\(refreshToken)&client_id=\(clientID)"
let refreshToken = try? keychainUtil.findPassword(PrefKeys.refreshToken.rawValue)

//defaults.string(forKey: PrefKeys.refreshToken.rawValue) ?? ""
let clientID = try? keychainUtil.findPassword(PrefKeys.clientID.rawValue)

//defaults.string(forKey: PrefKeys.clientID.rawValue) ?? ""

var parameters = "grant_type=refresh_token&refresh_token=\(refreshToken ?? "")&client_id=\(clientID ?? "")"
if let clientSecret = defaults.string(forKey: PrefKeys.clientSecret.rawValue) {
parameters.append("&client_secret=\(clientSecret)")
}
Expand All @@ -64,9 +70,15 @@ class TokenManager {

let json = try decoder.decode(RefreshTokenResponse.self, from: data)
let expirationDate = Date().addingTimeInterval(TimeInterval(Int(json.expiresIn) ?? 0))
UserDefaults.standard.set(expirationDate, forKey: PrefKeys.expirationDate.rawValue)
UserDefaults.standard.set(json.refreshToken, forKey: PrefKeys.refreshToken.rawValue)
UserDefaults.standard.set(json.accessToken, forKey: PrefKeys.accessToken.rawValue)
let keychainUtil = KeychainUtil()

let _ = keychainUtil.setPassword(PrefKeys.expirationDate.rawValue, pass: expirationDate.ISO8601Format())

let _ = keychainUtil.setPassword(PrefKeys.refreshToken.rawValue, pass: json.refreshToken)


let _ = keychainUtil.setPassword(PrefKeys.accessToken.rawValue, pass: json.accessToken)

completion(true,false)

}
Expand Down
2 changes: 1 addition & 1 deletion XCreds/VerifyOIDCPasswordWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Cocoa

class VerifyOIDCPassword: NSWindowController {
class VerifyOIDCPasswordWindowController: NSWindowController {

@IBOutlet weak var passwordTextField: NSSecureTextField!

Expand Down
15 changes: 9 additions & 6 deletions XCreds/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,18 @@ extension WebViewController: OIDCLiteDelegate {

func tokenResponse(tokens: OIDCLiteTokenResponse) {

UserDefaults.standard.set(tokens.accessToken, forKey: PrefKeys.accessToken.rawValue)
UserDefaults.standard.set(tokens.idToken, forKey: PrefKeys.idToken.rawValue)
UserDefaults.standard.set(tokens.refreshToken, forKey: PrefKeys.refreshToken.rawValue)


RunLoop.main.perform {
self.window?.close()
if let password = self.password {
NotificationCenter.default.post(name: Notification.Name("TCSTokensUpdated"), object: self, userInfo: ["password":password])
NotificationCenter.default.post(name: Notification.Name("TCSTokensUpdated"), object: self, userInfo:
[
"password":password,
PrefKeys.accessToken.rawValue:tokens.accessToken ?? "",
PrefKeys.idToken.rawValue:tokens.idToken ?? "",
PrefKeys.refreshToken.rawValue:tokens.refreshToken ?? ""

]
)

}
}
Expand Down
4 changes: 2 additions & 2 deletions xCreds.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@
CODE_SIGN_ENTITLEMENTS = XCreds/xCreds.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1251;
CURRENT_PROJECT_VERSION = 1252;
DEVELOPMENT_TEAM = UXP6YEHSPW;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -394,7 +394,7 @@
CODE_SIGN_ENTITLEMENTS = XCreds/xCreds.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1251;
CURRENT_PROJECT_VERSION = 1252;
DEVELOPMENT_TEAM = UXP6YEHSPW;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,22 @@
uuid = "94C20054-8AB3-42DB-93A7-A3026166D6FC"
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6DC42B2C-6769-4F0B-BE0A-F50363F8E31E"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/MainController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "26"
endingLineNumber = "26"
landmarkName = "run()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

0 comments on commit a2ebf9f

Please sign in to comment.