-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAuthenticationFlows.swift
More file actions
131 lines (102 loc) · 4.53 KB
/
Copy pathAuthenticationFlows.swift
File metadata and controls
131 lines (102 loc) · 4.53 KB
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
//
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//
import UIKit
import BrowserSignin
let issuer = "https://<#domain#>"
let clientId = "<#clientId#>"
let redirectUri = "<#redirectUri#>"
func signInWithWeb() async throws {
// Sign in using the default configuration
let token = try await BrowserSignin.shared?.signIn(from: view.window)
// Save the user's tokens
try Credential.store(token)
}
func signInWithWebUsingCustomConfiguration() async throws {
guard let issuerUrl = URL(string: issuer),
let redirectUrl = URL(string: redirectUri)
else {
throw SampleError.invalidUrl
}
let auth = BrowserSignin(issuerURL: issuerUrl,
clientId: clientId,
scope: "openid profile email offline_access device_sso",
redirectUri: redirectUrl)
// Sign in using the above configuration
let token = try await auth.signIn(from: view.window)
// Save the user's tokens
let credential = try Credential.store(token)
}
func signInUsingAuthorizationCode() async throws {
guard let issuerUrl = URL(string: issuer),
let redirectUrl = URL(string: redirectUri)
else {
throw SampleError.invalidUrl
}
let flow = AuthorizationCodeFlow(issuerURL: issuerUrl,
clientId: clientId,
scope: "openid profile email offline_access",
redirectUri: redirectUrl)
// Initiate the auth flow, and get the URL to present to the user
let authorizeUrl = try await flow.start()
// Open that URL in a browser, and wait for the redirect
let redirectURL: URL // Get the URL from the browser redirect
// Exchange the redirect URL for a token
let token = try await flow.resume(with: redirectURL)
// Save the user's tokens
let credential = try Credential.store(token)
}
func signInUsingResourceOwner(username: String, password: String) async throws {
guard let issuerUrl = URL(string: issuer) else {
throw SampleError.invalidUrl
}
let flow = ResourceOwnerFlow(issuerURL: issuerUrl,
clientId: clientId,
scope: "openid profile email offline_access")
// Sign in using a username & password
let token = try await flow.start(username: username, password: password)
// Save the user's tokens
let credential = try Credential.store(token)
}
func signInUsingDeviceSSO(deviceToken: String, idToken: String) async throws {
guard let issuerUrl = URL(string: issuer) else {
throw SampleError.invalidUrl
}
// Create the flow
let flow = TokenExchangeFlow(issuerURL: issuerUrl,
clientId: clientId,
scope: "openid profile offline_access")
// Exchange the ID and Device tokens for access tokens.
let token = try await flow.start(with: [
.actor(type: .deviceSecret, value: deviceToken),
.subject(type: .idToken, value: idToken)
])
// Save the user's token
let credential = try Credential.store(token)
}
func signInUsingDeviceAuthorizationCode() async throws {
guard let issuerUrl = URL(string: issuer) else {
throw SampleError.invalidUrl
}
let flow = DeviceAuthorizationFlow(issuer: issuerUrl,
clientId: clientId,
scopes: "openid profile email offline_access")
// Initiate the auth flow, and get the user code to display to the user
let context = try await flow.start()
print("Go to \(context.verificationUri.absoluteString) and enter \(context.userCode)")
// Poll the server, waiting for a successful login
let token = try await flow.resume(with: context)
// Save the user's tokens
let credential = try Credential.store(token)
}
enum SampleError: Error {
case invalidUrl
}