-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSessionManager.swift
152 lines (104 loc) · 4.21 KB
/
SessionManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// SessionManager.swift
// NoMAD-ADAuth
//
// Created by Joel Rennich on 11/10/17.
// Copyright © 2018 Orchard & Grove Inc. All rights reserved.
//
import Foundation
//import NoMADPRIVATE
// what we're keeping track of for every user
public struct NoMADSessionUserObject {
var userPrincipal: String
var session: NoMADSession
var aging: Bool
var expiration: Date?
var daysToGo: Int?
var userInfo: ADUserRecord?
}
// class to keep track and manage multiple AD sessions simultaneously
public class SessionManager: NoMADUserSessionDelegate {
/// The default instance of `SessionManager` to be used.
public static let shared = SessionManager()
public var sessions = [String : NoMADSessionUserObject]()
let dateFormatter = DateFormatter()
let myWorkQueue = DispatchQueue(label: "menu.nomad.NoMADADAuth.sessionmanager.background_work_queue", attributes: [])
init() {
// a bit more setup
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
// get all of the current principals with tickets
self.getList()
}
// udpate the list
public func update(user : String) {
if sessions[user] == nil {
// We don't know about this user yet
return
}
sessions[user]?.session.delegate = self
let _ = sessions[user]?.session.getUserInformation()
}
// updates all known users
public func updateAll() {
if sessions.count < 1 {
// no sessions so return
return
}
for session in sessions {
session.value.session.delegate = self
let _ = session.value.session.getUserInformation()
}
}
// gets new list of users
public func getList() {
klistUtil.klist()
let principals = klistUtil.returnPrincipals()
if principals.count > 0 {
for user in principals {
if sessions[user] == nil {
// add the account
let userSession = NoMADSession.init(domain: user.components(separatedBy: "@").last?.lowercased() ?? "", user: user, type: .AD)
myWorkQueue.async {
userSession.delegate = self
userSession.userInfo()
}
sessions[user] = NoMADSessionUserObject.init(userPrincipal: user, session: userSession, aging: false, expiration: nil, daysToGo: nil, userInfo: nil)
}
}
}
}
// manually adds a user with a session
public func createEntry(user : String, session : NoMADSession, update: Bool=true) {
sessions[user] = NoMADSessionUserObject.init(userPrincipal: user, session: session, aging: false, expiration: nil, daysToGo: nil, userInfo: nil)
if update {
// update the information
session.delegate = self
let _ = session.getUserInformation()
}
}
// update a NoMADSessionUserObject object
public func updateUser(user : String) {
}
// Add a new session to the list
// PRAGMA: Auth callbacks
public func NoMADAuthenticationSucceded() {
// we'll never auth here
}
public func NoMADAuthenticationFailed(error: NoMADSessionError, description: String) {
// we'll never auth here
}
public func NoMADUserInformation(user: ADUserRecord) {
// we shouldn't not already know about this user, but we'll double check
if sessions[user.userPrincipal] == nil {
return
}
if user.passwordExpire != nil && user.passwordAging! {
sessions[user.userPrincipal]?.daysToGo = Int((user.passwordExpire?.timeIntervalSince(Date()))!)/86400
sessions[user.userPrincipal]?.expiration = user.passwordExpire
sessions[user.userPrincipal]?.aging = true
} else {
sessions[user.userPrincipal]?.aging = false
}
}
}