Skip to content

Commit

Permalink
fixed status icon issue; fixed lack of prompting on first launch
Browse files Browse the repository at this point in the history
  • Loading branch information
twocanoes committed Aug 5, 2022
1 parent de4acf0 commit 9aa2d77
Show file tree
Hide file tree
Showing 23 changed files with 479 additions and 130 deletions.
50 changes: 50 additions & 0 deletions Profile Manifest/com.twocanoes.xcreds.plist
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,57 @@ A profile can consist of payloads with different version numbers. For example, c
<key>pfm_default</key>
<true/>
</dict>
<dict>
<key>pfm_name</key>
<string>shouldShowVersionInfo</string>
<key>pfm_type</key>
<string>boolean</string>
<key>pfm_title</key>
<string>Show Version and Build Number</string>
<key>pfm_description</key>
<string>Show the version number and build number in the lower left corner of XCreds Login.</string>
<key>pfm_default</key>
<true/>
</dict>

<dict>
<key>pfm_name</key>
<string>shouldShowSupportStatus</string>
<key>pfm_type</key>
<string>boolean</string>
<key>pfm_title</key>
<string>Show Support Status Message</string>
<key>pfm_description</key>
<string>Show message in XCreds Login reminding people to buy support.</string>
<key>pfm_default</key>
<true/>
</dict>

<dict>
<key>pfm_name</key>
<string>shouldShowConfigureWifiButton</string>
<key>pfm_type</key>
<string>boolean</string>
<key>pfm_title</key>
<string>Show Configure WiFi</string>
<key>pfm_description</key>
<string>Show Configure WiFi button in XCreds Login.</string>
<key>pfm_default</key>
<true/>
</dict>

<dict>
<key>pfm_name</key>
<string>shouldShowMacLoginButton</string>
<key>pfm_type</key>
<string>boolean</string>
<key>pfm_title</key>
<string>Show Mac Login Window button</string>
<key>pfm_description</key>
<string>Show the Mac Login Window button in XCreds Login.</string>
<key>pfm_default</key>
<true/>
</dict>
<dict>
<key>pfm_name</key>
<string>shouldSetGoogleAccessTypeToOffline</string>
Expand Down
2 changes: 1 addition & 1 deletion XCreds/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"author" : "xcode",
"version" : 1
}
}
}
23 changes: 23 additions & 0 deletions XCreds/Assets.xcassets/wifi.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
},
"images" : [
{
"filename" : "wifi~universal@1x.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "wifi~universal@2x.png",
"scale" : "2x",
"idiom" : "universal"
},
{
"scale" : "3x",
"filename" : "wifi~universal@3x.png",
"idiom" : "universal"
}
]
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions XCreds/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<items>
<menuItem title="xCreds" id="1Xt-HY-uBw">
<menuItem title="XCreds" id="1Xt-HY-uBw">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="xCreds" systemMenu="apple" id="uQy-DD-JDr">
<menu key="submenu" title="XCreds" systemMenu="apple" id="uQy-DD-JDr">
<items>
<menuItem title="About xCreds" id="5kV-Vb-QxS">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
9 changes: 9 additions & 0 deletions XCreds/MainMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class MainMenu: NSObject, NSMenuDelegate {
var menuOpen = false // is the menu open?
var menuBuilt: Date? // last time menu was built

var signedIn = false

let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

// windows
Expand Down Expand Up @@ -69,6 +71,13 @@ class MainMenu: NSObject, NSMenuDelegate {
mainMenu.addItem(quitMenuItem)
}

if signedIn == true {
statusBarItem.button?.image=NSImage(named: "xcreds menu icon check")

}
else {
statusBarItem.button?.image=NSImage(named: "xcreds menu icon")
}
}

//MARK: NSMenuDelegate
Expand Down
2 changes: 1 addition & 1 deletion XCreds/PrefKeys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

enum PrefKeys: String {
case clientID, clientSecret, password="local password",discoveryURL, redirectURI, scopes, accessToken, idToken, refreshToken, tokenEndpoint, expirationDate, invalidToken, refreshRateHours, showDebug, verifyPassword, shouldShowQuitMenu, shouldShowPreferencesOnStart, shouldSetGoogleAccessTypeToOffline, passwordChangeURL, shouldShowAboutMenu, username, customURL, customPasswordElementID
case clientID, clientSecret, password="local password",discoveryURL, redirectURI, scopes, accessToken, idToken, refreshToken, tokenEndpoint, expirationDate, invalidToken, refreshRateHours, showDebug, verifyPassword, shouldShowQuitMenu, shouldShowPreferencesOnStart, shouldSetGoogleAccessTypeToOffline, passwordChangeURL, shouldShowAboutMenu, username, customURL, customPasswordElementID, shouldShowVersionInfo, shouldShowSupportStatus,shouldShowConfigureWifiButton, shouldShowMacLoginButton
}
func getManagedPreference(key: Preferences) -> Any? {

Expand Down
3 changes: 2 additions & 1 deletion XCreds/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ class PreferencesWindowController: NSWindowController {
let _ = keychainUtil.findAndDelete(PrefKeys.accessToken.rawValue)
let _ = keychainUtil.findAndDelete(PrefKeys.idToken.rawValue)
let _ = keychainUtil.findAndDelete(PrefKeys.refreshToken.rawValue)
mainMenu.statusBarItem.button?.image=NSImage(named: "xcreds menu icon")


mainMenu.signedIn=false
mainMenu.buildMenu()
}

}
15 changes: 11 additions & 4 deletions XCreds/ScheduleManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ class ScheduleManager {
}
}
func checkToken() {
// // we have not resolved the tokenEndpoint yet, so pop up a window
// if UserDefaults.standard.string(forKey: PrefKeys.tokenEndpoint.rawValue) == nil {
// DispatchQueue.main.async {
// SignInMenuItem().doAction()
// }
// return
// }

TokenManager.shared.getNewAccessToken(completion: { isSuccessful, hadConnectionError in

if hadConnectionError==true {
Expand All @@ -49,7 +57,8 @@ class ScheduleManager {
NotifyManager.shared.sendMessage(message: "Azure password unchanged")
}
DispatchQueue.main.async {
mainMenu.statusBarItem.button?.image=NSImage(named: "xcreds menu icon check")
mainMenu.signedIn=true
mainMenu.buildMenu()
}


Expand All @@ -61,9 +70,7 @@ class ScheduleManager {
NotifyManager.shared.sendMessage(message: "Azure password changed or not set")
}
DispatchQueue.main.async {
// mainMenu.webView = WebViewController()
// mainMenu.webView?.window!.forceToFrontAndFocus(nil)
// mainMenu.webView?.run()

SignInMenuItem().doAction()
}

Expand Down
8 changes: 7 additions & 1 deletion XCreds/SignInMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class SignInMenuItem: NSMenuItem {

override var title: String {
get {
"Sign In..."
if mainMenu.signedIn==true {
return "Refresh..."
}
else {
return "Sign In..."
}

}
set {
return
Expand Down
50 changes: 48 additions & 2 deletions XCreds/TokenManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Timothy Perfitt on 4/5/22.
//
import Foundation

import OIDCLite

struct RefreshTokenResponse: Codable {
let accessToken, expiresIn, expiresOn, refreshToken, extExpiresIn,tokenType: String
Expand Down Expand Up @@ -37,6 +37,38 @@ class TokenManager {

let defaults = UserDefaults.standard
var timer: Timer?
private var oidcLocal:OIDCLite?
func oidc() -> OIDCLite {
var scopes: [String]?

var additionalParameters:[String:String]? = nil
var clientSecret:String?

if let oidcPrivate = oidcLocal {
return oidcPrivate
}
if let clientSecretRaw = UserDefaults.standard.string(forKey: PrefKeys.clientSecret.rawValue),
clientSecretRaw != "" {
clientSecret = clientSecretRaw
}

if let scopesRaw = UserDefaults.standard.string(forKey: PrefKeys.scopes.rawValue) {
scopes = scopesRaw.components(separatedBy: " ")
}
//

if UserDefaults.standard.bool(forKey: PrefKeys.shouldSetGoogleAccessTypeToOffline.rawValue) == true {
additionalParameters = ["access_type":"offline", "prompt":"consent"]
}

let oidcLite = OIDCLite(discoveryURL: UserDefaults.standard.string(forKey: PrefKeys.discoveryURL.rawValue) ?? "NONE", clientID: UserDefaults.standard.string(forKey: PrefKeys.clientID.rawValue) ?? "NONE", clientSecret: clientSecret, redirectURI: UserDefaults.standard.string(forKey: PrefKeys.redirectURI.rawValue), scopes: scopes, additionalParameters:additionalParameters )
oidcLite.getEndpoints()

oidcLocal = oidcLite
return oidcLite


}

func saveTokensToKeychain(tokens:Tokens, setACL:Bool=false, password:String?=nil) -> Bool {
let keychainUtil = KeychainUtil()
Expand Down Expand Up @@ -84,10 +116,24 @@ class TokenManager {
}
return true
}
func tokenEndpoint() -> String? {

let prefTokenEndpoint = UserDefaults.standard.string(forKey: PrefKeys.tokenEndpoint.rawValue)
if prefTokenEndpoint != nil {
return prefTokenEndpoint
}


if let tokenEndpoint = oidc().OIDCTokenEndpoint {
UserDefaults.standard.set(tokenEndpoint, forKey: PrefKeys.tokenEndpoint.rawValue)
return tokenEndpoint
}
return nil
}
func getNewAccessToken(completion:@escaping (_ isSuccessful:Bool,_ hadConnectionError:Bool)->Void) -> Void {

guard let url = URL(string: defaults.string(forKey: PrefKeys.tokenEndpoint.rawValue) ?? "") else {

guard let endpoint = TokenManager.shared.tokenEndpoint(), let url = URL(string: endpoint) else {
completion(false,true)
return
}
Expand Down
73 changes: 37 additions & 36 deletions XCreds/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,41 @@ class WebViewController: NSWindowController {
@IBOutlet weak var webView: WKWebView!
@IBOutlet weak var cancelButton: NSButton!

var oidcLite: OIDCLite?
var password:String?
func loadPage() {
//
// var scopes: [String]?
// var clientSecret: String?
//
// if let clientSecretRaw = UserDefaults.standard.string(forKey: PrefKeys.clientSecret.rawValue),
// clientSecretRaw != "" {
// clientSecret = clientSecretRaw
// }
//
// if let scopesRaw = UserDefaults.standard.string(forKey: PrefKeys.scopes.rawValue) {
// scopes = scopesRaw.components(separatedBy: " ")
// }
// //
// var additionalParameters:[String:String]? = nil
//
// if UserDefaults.standard.bool(forKey: PrefKeys.shouldSetGoogleAccessTypeToOffline.rawValue) == true {
// additionalParameters = ["access_type":"offline", "prompt":"consent"]
// }
// TCSLogWithMark("redirect URI: \(UserDefaults.standard.string(forKey: PrefKeys.redirectURI.rawValue) ?? "NONE")")
// oidcLite = OIDCLite(discoveryURL: UserDefaults.standard.string(forKey: PrefKeys.discoveryURL.rawValue) ?? "NONE", clientID: UserDefaults.standard.string(forKey: PrefKeys.clientID.rawValue) ?? "NONE", clientSecret: clientSecret, redirectURI: UserDefaults.standard.string(forKey: PrefKeys.redirectURI.rawValue), scopes: scopes, additionalParameters:additionalParameters )
// webView.navigationDelegate = self
// oidcLite?.delegate = self
// oidcLite?.getEndpoints()
//
// if let tokenEndpoint = oidcLite?.OIDCTokenEndpoint {
// UserDefaults.standard.set(tokenEndpoint, forKey: PrefKeys.tokenEndpoint.rawValue)
// }
webView.navigationDelegate = self

var scopes: [String]?
var clientSecret: String?

if let clientSecretRaw = UserDefaults.standard.string(forKey: PrefKeys.clientSecret.rawValue),
clientSecretRaw != "" {
clientSecret = clientSecretRaw
}

if let scopesRaw = UserDefaults.standard.string(forKey: PrefKeys.scopes.rawValue) {
scopes = scopesRaw.components(separatedBy: " ")
}
//
var additionalParameters:[String:String]? = nil

if UserDefaults.standard.bool(forKey: PrefKeys.shouldSetGoogleAccessTypeToOffline.rawValue) == true {
additionalParameters = ["access_type":"offline", "prompt":"consent"]
}
TCSLogWithMark("redirect URI: \(UserDefaults.standard.string(forKey: PrefKeys.redirectURI.rawValue) ?? "NONE")")
oidcLite = OIDCLite(discoveryURL: UserDefaults.standard.string(forKey: PrefKeys.discoveryURL.rawValue) ?? "NONE", clientID: UserDefaults.standard.string(forKey: PrefKeys.clientID.rawValue) ?? "NONE", clientSecret: clientSecret, redirectURI: UserDefaults.standard.string(forKey: PrefKeys.redirectURI.rawValue), scopes: scopes, additionalParameters:additionalParameters )
webView.navigationDelegate = self
oidcLite?.delegate = self
oidcLite?.getEndpoints()

if let tokenEndpoint = oidcLite?.OIDCTokenEndpoint {
UserDefaults.standard.set(tokenEndpoint, forKey: PrefKeys.tokenEndpoint.rawValue)
}

TokenManager.shared.oidc().delegate = self
clearCookies()

if let url = oidcLite?.createLoginURL() {
if let url = TokenManager.shared.oidc().createLoginURL() {
self.webView.load(URLRequest(url: url))
}
}
Expand Down Expand Up @@ -172,10 +173,10 @@ extension WebViewController: WKNavigationDelegate {
TCSLogWithMark(navigationAction.request.httpMethod ?? "Unknown method")
TCSLogWithMark("path = \(navigationAction.request.url?.path ?? "no path")");

let javaScript = "document.documentElement.outerHTML.toString()"
webView.evaluateJavaScript(javaScript, completionHandler: { response, error in
TCSLogWithMark(response as? String ?? "No HTML")
})
// let javaScript = "document.documentElement.outerHTML.toString()"
// webView.evaluateJavaScript(javaScript, completionHandler: { response, error in
// TCSLogWithMark(response as? String ?? "No HTML")
// })


}
Expand All @@ -194,7 +195,7 @@ extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
TCSLogWithMark("WebDel:: Did Receive Redirect for: \(webView.url?.absoluteString ?? "None")")

if let redirectURI = oidcLite?.redirectURI {
let redirectURI = TokenManager.shared.oidc().redirectURI
TCSLogWithMark("redirectURI: \(redirectURI)")
TCSLogWithMark("URL: \(webView.url?.absoluteString ?? "NONE")")
if (webView.url?.absoluteString.starts(with: (redirectURI))) ?? false {
Expand All @@ -204,12 +205,12 @@ extension WebViewController: WKNavigationDelegate {
for part in pathParts {
if part.contains("code=") {
code = part.replacingOccurrences(of: redirectURI + "?" , with: "").replacingOccurrences(of: "code=", with: "")
oidcLite?.getToken(code: code)
TokenManager.shared.oidc().getToken(code: code)
return
}
}
}
}

}

private func queryToDict(query: String) -> [String:String]? {
Expand Down
8 changes: 8 additions & 0 deletions XCreds/defaults.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@
<true/>
<key>shouldShowQuitMenu</key>
<true/>
<key>shouldShowVersionInfo</key>
<true/>
<key>shouldShowSupportStatus</key>
<string>1</string>
<key>shouldShowConfigureWifiButton</key>
<true/>
<key>shouldShowMacLoginButton</key>
<string>1</string>
</dict>
</plist>

0 comments on commit 9aa2d77

Please sign in to comment.