-
Notifications
You must be signed in to change notification settings - Fork 2
/
MSAuthAdapter.swift
197 lines (163 loc) · 6.51 KB
/
MSAuthAdapter.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// MSAuthAdapter.swift
// ace-life
//
// Created by Alexander Schmutz on 07.08.21.
//
// Copyright (c) Microsoft Corporation.
// All rights reserved.
//
import UIKit
import OSLog
protocol MSAuthAdapterProtocol {
func setupViewConnection()
func login(withInteraction: Bool)
func logout()
func openUrl(url: URL)
func loadDeviceMode()
}
class MSAuthAdapter: MSAuthAdapterProtocol {
private let msGraphEndpoint = "https://graph.microsoft.com"
private let msAuthority: String = "https://login.microsoftonline.com/\(MSAuthCredentials.directoryId)"
private let msRedirectUri = "msauth.\(Bundle.main.bundleIdentifier!)://auth"
private let msScopes: [String] = ["User.Read"]
private let appState: MSAuthState
private let msAuthProxy: MSAuthProxyProtocol
private var accessToken: String? // TODO: Use keychain to store accessToken
init(
appState: MSAuthState = resolve(),
msAuthProxy: MSAuthProxyProtocol = resolve()
) {
self.appState = appState
self.msAuthProxy = msAuthProxy
setupApplicationContext()
}
/// needs to be called after the view was initialised so the topViewController can be found
func setupViewConnection() {
if let topViewController = UIApplication.topViewController {
msAuthProxy.connectWith(viewController: topViewController)
}
}
func login(withInteraction: Bool) {
loadAccount { account in
if account != nil {
self.acquireTokenSilently()
} else if withInteraction {
self.acquireTokenInteractively()
} else {
self.logout()
}
}
}
func logout() {
msAuthProxy.logout { error in
if let error = error {
Logger.msAuthentication.error("Couldn't sign out account with error: \(String(describing: error))")
self.resetState()
return
}
Logger.msAuthentication.debug("Sign out completed successfully")
self.resetState()
}
}
func openUrl(url: URL) {
msAuthProxy.openUrl(url: url)
}
private func setupApplicationContext() {
guard let authorityURL = URL(string: msAuthority) else {
Logger.msAuthentication.error("Unable to create authority URL")
return
}
do {
try msAuthProxy.setupApplicationContext(msAuthorityUrl: authorityURL, msRedirectUri: msRedirectUri)
} catch let error {
Logger.msAuthentication.error("Unable to create Application Context: \(String(describing: error))")
}
}
private func loadAccount(completion: ((Account?) -> Void)? = nil) {
msAuthProxy.loadAccount { account, error in
if let error = error {
Logger.msAuthentication.error("Couldn't query current account with error: \(String(describing: error))")
return
}
if let account = account {
Logger.msAuthentication.error("Found a signed in account \(account.email ?? ""). Updating data for that account...")
completion?(account)
return
}
Logger.msAuthentication.debug("Account signed out.")
self.resetState()
completion?(nil)
}
}
private func resetState() {
setAccount(nil)
}
private func setAccount(_ account: Account?) {
DispatchQueue.main.async {
self.appState.account = account
}
}
private func acquireTokenInteractively() {
msAuthProxy.acquireTokenInteractively(msScopes: msScopes) { account, accessToken, error in
if let error = error {
Logger.msAuthentication.error("Could not acquire token: \(String(describing: error))")
return
}
guard let account = account, let accessToken = accessToken else {
Logger.msAuthentication.error("Could not acquire token: No result returned")
return
}
self.accessToken = accessToken
Logger.msAuthentication.debug("Aquired access token interactively")
self.setAccount(account)
self.loadContentWithToken(accessToken: accessToken)
}
}
private func acquireTokenSilently() {
msAuthProxy.acquireTokenSilently(msScopes: msScopes) { account, accessToken, error in
if let error = error {
if let error = error as? MSAuthError, error == .interactiveLoginRequired {
DispatchQueue.main.async {
self.acquireTokenInteractively()
}
} else {
Logger.msAuthentication.error("Could not acquire token silently: \(String(describing: error))")
}
return
}
guard let account = account, let accessToken = accessToken else {
Logger.msAuthentication.error("Could not acquire token: No result returned")
return
}
self.accessToken = accessToken
self.setAccount(account)
self.loadContentWithToken(accessToken: accessToken)
Logger.msAuthentication.debug("Refreshed Access token")
}
}
func loadDeviceMode() {
msAuthProxy.deviceMode { deviceMode, error in
if let deviceMode = deviceMode {
Logger.msAuthentication.log("Received device info. Device is in the \(deviceMode) mode.")
} else {
Logger.msAuthentication.error("Device info not returned. Error: \(String(describing: error))")
}
}
}
private func loadContentWithToken(accessToken: String) {
var request = URLRequest(url: URL(string: msGraphEndpoint + "/v1.0/me/")!)
request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
URLSession.shared.dataTask(with: request) { data, _, error in
if let error = error {
Logger.msAuthentication.error("Couldn't get graph result: \(String(describing: error))")
return
}
guard let result = try? JSONSerialization.jsonObject(with: data!, options: []) else {
Logger.msAuthentication.error("Couldn't deserialize result JSON")
return
}
Logger.msAuthentication.log("Result from Graph: \(String(describing: result)))")
}.resume()
}
}