forked from aerogear/aerogear-ios-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.swift
127 lines (112 loc) · 4.31 KB
/
Config.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
/*
* JBoss, Home of Professional Open Source.
* Copyright Red Hat, Inc., and individual contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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
public class Config {
/**
* Applies the baseURL to the configuration.
*/
public let base: String
public var baseURL:NSURL {
get {
return NSURL.URLWithString(base)
}
}
/**
* Applies the "authorization endpoint" to the request token.
*/
public let authzEndpoint: String
public var authzEndpointURL: NSURL {
get {
if authzEndpoint.hasPrefix("http") {
return NSURL(string: authzEndpoint)
} else {
var formattedEndpoint = authzEndpoint.hasPrefix("/") ? (authzEndpoint as NSString).substringFromIndex(1) : authzEndpoint
return baseURL.URLByAppendingPathComponent(formattedEndpoint)
}
}
}
/**
* Applies the "callback URL" once request token issued.
*/
public let redirectURL: String
/**
* Applies the "access token endpoint" to the exchange code for access token.
*/
public let accessTokenEndpoint: String
/**
* Computed property to get URL by taking care of extra or missing pre or postfix '/'.
*/
public var accessTokenEndpointURL: NSURL {
get {
if accessTokenEndpoint.hasPrefix("http") {
return NSURL(string: accessTokenEndpoint)
} else {
var formattedEndpoint = accessTokenEndpoint.hasPrefix("/") ? (accessTokenEndpoint as NSString).substringFromIndex(1) : accessTokenEndpoint
return baseURL.URLByAppendingPathComponent(formattedEndpoint)
}
}
}
/**
* Endpoint for request to invalidate both accessToken and refreshToken.
*/
public let revokeTokenEndpoint: String?
/**
* Computed property to get URL by taking care of extra or missing pre or postfix '/'.
*/
public var revokeTokenEndpointURL: NSURL? {
get {
if let unwrappedRevokeTokenEndpoint = revokeTokenEndpoint {
if (revokeTokenEndpoint != nil && revokeTokenEndpoint!.hasPrefix("http")) {
return NSURL(string: revokeTokenEndpoint!)
} else {
var formattedEndpoint = unwrappedRevokeTokenEndpoint.hasPrefix("/") ? (unwrappedRevokeTokenEndpoint as NSString).substringFromIndex(1) : unwrappedRevokeTokenEndpoint
return baseURL.URLByAppendingPathComponent(formattedEndpoint)
}
} else {
return nil
}
}
}
/**
* Applies the various scopes of the authorization.
*/
public let scopes: [String]
/**
* Applies the "client id" obtained with the client registration process.
*/
public let clientId: String
/**
* Applies the "client secret" obtained with the client registration process.
*/
public let clientSecret: String?
/**
* Account id is used with AccountManager to store tokens. AccountId is defined by the end-user
* and can be any String. If AccountManager is not used, this field is optional.
*/
public let accountId: String?
public init(base: String, authzEndpoint: String, redirectURL: String, accessTokenEndpoint: String, clientId: String, revokeTokenEndpoint: String? = nil, scopes: [String] = [], clientSecret: String? = nil, accountId: String? = nil) {
self.base = base
self.authzEndpoint = authzEndpoint
self.redirectURL = redirectURL
self.accessTokenEndpoint = accessTokenEndpoint
self.revokeTokenEndpoint = revokeTokenEndpoint
self.scopes = scopes
self.clientId = clientId
self.clientSecret = clientSecret
self.accountId = accountId
}
}