Skip to content

Commit

Permalink
added refresh rate and cleaned up prefs pane
Browse files Browse the repository at this point in the history
  • Loading branch information
twocanoes committed Jun 4, 2022
1 parent a2ebf9f commit d666ab2
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 155 deletions.
19 changes: 14 additions & 5 deletions XCreds/MainController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import Cocoa

class MainController: NSObject {
func run() -> Void {

let defaultsPath = Bundle.main.path(forResource: "defaults", ofType: "plist")

if let defaultsPath = defaultsPath {

let defaultsDict = NSDictionary(contentsOfFile: defaultsPath)
UserDefaults.standard.register(defaults: defaultsDict as! [String : Any])
}

// make sure we have the local password, else prompt. we don't need to save it
// just make sure we prompt if not in the keychain. if the user cancels, then it will
// prompt when using OAuth.
Expand All @@ -24,15 +33,15 @@ class MainController: NSObject {
}

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

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

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


Expand Down Expand Up @@ -103,8 +112,8 @@ class MainController: NSObject {
let isPasswordValid = PasswordUtils.verifyCurrentUserPassword(password:localPassword )
if isPasswordValid==true {
passwordWindowController.window?.close()
let err = keychainUtil.setPassword("local password", pass: localPassword)
if err != OSStatus(errSecSuccess) {
let err = keychainUtil.updatePassword("local password", pass: localPassword)
if err == false {
return nil
}
return localPassword
Expand Down
2 changes: 1 addition & 1 deletion XCreds/PrefKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
import Foundation

enum PrefKeys: String {
case clientID, clientSecret, discoveryURL, redirectURI, scopes, accessToken, idToken, refreshToken, tokenEndpoint, expirationDate, invalidToken
case clientID, clientSecret, discoveryURL, redirectURI, scopes, accessToken, idToken, refreshToken, tokenEndpoint, expirationDate, invalidToken, refreshRate
}
160 changes: 26 additions & 134 deletions XCreds/PreferencesWindow.xib

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion XCreds/ScheduleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ class ScheduleManager {
return
}

timer=Timer.scheduledTimer(withTimeInterval: 5, repeats: true, block: { timer in
var rate = UserDefaults.standard.integer(forKey: PrefKeys.refreshRate.rawValue)

if rate < 5 {
rate = 5
}
timer=Timer.scheduledTimer(withTimeInterval: TimeInterval(rate), repeats: true, block: { timer in
TokenManager.shared.getNewAccessToken(completion: { isSuccessful, hadConnectionError in

if hadConnectionError==true {
Expand Down
15 changes: 5 additions & 10 deletions XCreds/TokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ class TokenManager {
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) ?? ""
let clientID = 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) {
Expand All @@ -70,14 +68,11 @@ class TokenManager {

let json = try decoder.decode(RefreshTokenResponse.self, from: data)
let expirationDate = Date().addingTimeInterval(TimeInterval(Int(json.expiresIn) ?? 0))
let keychainUtil = KeychainUtil()

let _ = keychainUtil.setPassword(PrefKeys.expirationDate.rawValue, pass: expirationDate.ISO8601Format())
UserDefaults.standard.set(expirationDate, forKey: PrefKeys.expirationDate.rawValue)

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


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

completion(true,false)

Expand Down
10 changes: 10 additions & 0 deletions XCreds/defaults.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http:https://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>redirectURI</key>
<string>xcreds:https://auth/</string>
<key>refreshRate</key>
<string>60</string>
</dict>
</plist>
8 changes: 6 additions & 2 deletions xCreds.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
764D8129284BCAB100B3EE54 /* Window+Shake.swift in Sources */ = {isa = PBXBuildFile; fileRef = 764D8128284BCAB100B3EE54 /* Window+Shake.swift */; };
764D812C284BCC7400B3EE54 /* VerifyOIDCPasswordWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 764D812A284BCC7400B3EE54 /* VerifyOIDCPasswordWindowController.swift */; };
764D812D284BCC7400B3EE54 /* VerifyOIDCPassword.xib in Resources */ = {isa = PBXBuildFile; fileRef = 764D812B284BCC7400B3EE54 /* VerifyOIDCPassword.xib */; };
764D812F284C06AB00B3EE54 /* defaults.plist in Resources */ = {isa = PBXBuildFile; fileRef = 764D812E284C06AB00B3EE54 /* defaults.plist */; };
767116A7284AABC500CCD6FF /* NotifyManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767116A6284AABC500CCD6FF /* NotifyManager.swift */; };
767116A9284AAE2B00CCD6FF /* ScheduleManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767116A8284AAE2B00CCD6FF /* ScheduleManager.swift */; };
767116AC284AB4C000CCD6FF /* PasswordUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 767116AB284AB4C000CCD6FF /* PasswordUtils.swift */; };
Expand Down Expand Up @@ -40,6 +41,7 @@
764D8128284BCAB100B3EE54 /* Window+Shake.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Window+Shake.swift"; sourceTree = "<group>"; };
764D812A284BCC7400B3EE54 /* VerifyOIDCPasswordWindowController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VerifyOIDCPasswordWindowController.swift; sourceTree = "<group>"; };
764D812B284BCC7400B3EE54 /* VerifyOIDCPassword.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = VerifyOIDCPassword.xib; sourceTree = "<group>"; };
764D812E284C06AB00B3EE54 /* defaults.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = defaults.plist; sourceTree = "<group>"; };
767116A6284AABC500CCD6FF /* NotifyManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotifyManager.swift; sourceTree = "<group>"; };
767116A8284AAE2B00CCD6FF /* ScheduleManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScheduleManager.swift; sourceTree = "<group>"; };
767116AB284AB4C000CCD6FF /* PasswordUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordUtils.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -111,6 +113,7 @@
76EE06B327FD1E5F009E0F3A /* WebView.swift */,
767116AB284AB4C000CCD6FF /* PasswordUtils.swift */,
76EE06B527FD1E79009E0F3A /* PreferencesWindow.xib */,
764D812E284C06AB00B3EE54 /* defaults.plist */,
767116AE284AB5D900CCD6FF /* XCreds-Bridging-Header.h */,
76EE06B727FD1EB7009E0F3A /* PreferencesWindowController.swift */,
76EE06B927FD1EE8009E0F3A /* SignInMenuItem.swift */,
Expand Down Expand Up @@ -195,6 +198,7 @@
files = (
76EE06B627FD1E79009E0F3A /* PreferencesWindow.xib in Resources */,
76EE06A027FD1D01009E0F3A /* Assets.xcassets in Resources */,
764D812F284C06AB00B3EE54 /* defaults.plist in Resources */,
764D812D284BCC7400B3EE54 /* VerifyOIDCPassword.xib in Resources */,
764D8127284BC1C300B3EE54 /* LoginPasswordWindowController.xib in Resources */,
76EE06A327FD1D01009E0F3A /* MainMenu.xib in Resources */,
Expand Down Expand Up @@ -365,7 +369,7 @@
CODE_SIGN_ENTITLEMENTS = XCreds/xCreds.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1252;
CURRENT_PROJECT_VERSION = 1253;
DEVELOPMENT_TEAM = UXP6YEHSPW;
ENABLE_HARDENED_RUNTIME = YES;
GENERATE_INFOPLIST_FILE = YES;
Expand Down Expand Up @@ -394,7 +398,7 @@
CODE_SIGN_ENTITLEMENTS = XCreds/xCreds.entitlements;
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1252;
CURRENT_PROJECT_VERSION = 1253;
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 @@ -7,16 +7,80 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6DC42B2C-6769-4F0B-BE0A-F50363F8E31E"
uuid = "DFC201A3-54C3-4E60-AA49-BAD35EB8D20B"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/TokenManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "getNewAccessToken(completion:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "38AC0916-97CA-44F6-9E5F-689EE26C9ABC"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/TokenManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "74"
endingLineNumber = "74"
landmarkName = "getNewAccessToken(completion:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "E88F8AB5-ABD1-4FF9-936A-330CC588B428"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/TokenManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "65"
endingLineNumber = "65"
landmarkName = "getNewAccessToken(completion:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "7713A147-4222-49E5-AFE0-D78342383967"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/MainController.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "19"
endingLineNumber = "19"
landmarkName = "run()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "A20FA142-934F-401A-91EF-5C93E8433521"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "XCreds/ScheduleManager.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "26"
endingLineNumber = "26"
landmarkName = "run()"
landmarkName = "startCredentialCheck()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down

0 comments on commit d666ab2

Please sign in to comment.