-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathAuthorizationCodeFlow+Requests.swift
More file actions
95 lines (81 loc) · 3.6 KB
/
Copy pathAuthorizationCodeFlow+Requests.swift
File metadata and controls
95 lines (81 loc) · 3.6 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
//
// Copyright (c) 2021-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 Foundation
import AuthFoundation
extension AuthorizationCodeFlow {
struct PushedAuthorizationResponse: Codable {
let requestUri: String
let expiresIn: Int
enum CodingKeys: String, CodingKey {
case requestUri = "request_uri"
case expiresIn = "expires_in"
}
}
struct PushedAuthorizationRequest: AuthenticationFlowRequest {
typealias Flow = AuthorizationCodeFlow
let url: URL
let clientConfiguration: OAuth2Client.Configuration
let additionalParameters: [String: any APIRequestArgument]?
let context: Flow.Context
}
struct TokenRequest: OAuth2TokenRequest, AuthenticationFlowRequest {
typealias Flow = AuthorizationCodeFlow
let openIdConfiguration: OpenIdConfiguration
let clientConfiguration: OAuth2Client.Configuration
let additionalParameters: [String: any APIRequestArgument]?
let context: Flow.Context
let authorizationCode: String
init(openIdConfiguration: OpenIdConfiguration,
clientConfiguration: OAuth2Client.Configuration,
additionalParameters: [String: any APIRequestArgument]?,
context: Context,
authorizationCode: String) throws
{
guard clientConfiguration.redirectUri != nil else {
throw OAuth2Error.redirectUriRequired
}
self.openIdConfiguration = openIdConfiguration
self.clientConfiguration = clientConfiguration
self.additionalParameters = additionalParameters
self.context = context
self.authorizationCode = authorizationCode
}
}
}
extension AuthorizationCodeFlow.TokenRequest {
var category: AuthFoundation.OAuth2APIRequestCategory { .token }
var tokenValidatorContext: any AuthenticationContext { context }
var bodyParameters: [String: any APIRequestArgument]? {
var result = additionalParameters ?? [:]
result.merge(clientConfiguration.parameters(for: category))
result.merge(context.parameters(for: category))
result.merge([
"grant_type": GrantType.authorizationCode,
GrantType.authorizationCode.responseKey: authorizationCode,
])
return result
}
}
extension AuthorizationCodeFlow.TokenRequest: APIParsingContext {}
extension AuthorizationCodeFlow.PushedAuthorizationRequest: APIRequest, APIRequestBody {
typealias ResponseType = AuthorizationCodeFlow.PushedAuthorizationResponse
var httpMethod: APIRequestMethod { .post }
var contentType: APIContentType? { .formEncoded }
var acceptsType: APIContentType? { .json }
var category: OAuth2APIRequestCategory { .authorization }
var bodyParameters: [String: any APIRequestArgument]? {
var result = additionalParameters ?? [:]
result.merge(clientConfiguration.parameters(for: category))
result.merge(context.parameters(for: category))
return result
}
}