Skip to content

Commit

Permalink
Replace in-line OAuth2 strings with centralized constants
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikPalo committed Nov 26, 2024
1 parent 2b38a87 commit d496a44
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
15 changes: 8 additions & 7 deletions Sources/Flows/OAuth2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#if os(macOS)
import macOS
#elseif os(iOS) || os(visionOS)
Expand Down Expand Up @@ -358,7 +359,7 @@ open class OAuth2: OAuth2Base {
}

let req = OAuth2AuthRequest(url: (clientConfig.refreshURL ?? clientConfig.tokenURL ?? clientConfig.authorizeURL))
req.params["grant_type"] = "refresh_token"
req.params["grant_type"] = OAuth2GrantTypes.refreshToken
req.params["refresh_token"] = refreshToken
if let clientId = clientId {
req.params["client_id"] = clientId
Expand Down Expand Up @@ -423,11 +424,11 @@ open class OAuth2: OAuth2Base {
}

let req = OAuth2AuthRequest(url: (clientConfig.tokenURL ?? clientConfig.authorizeURL))
req.params["grant_type"] = "urn:ietf:params:oauth:grant-type:token-exchange"
req.params["grant_type"] = OAuth2GrantTypes.tokenExchange
req.params["audience"] = audienceClientId
req.params["requested_token_type"] = "urn:ietf:params:oauth:token-type:refresh_token"
req.params["requested_token_type"] = OAuth2TokenTypeIdentifiers.refreshToken
req.params["subject_token"] = refreshToken
req.params["subject_token_type"] = "urn:ietf:params:oauth:token-type:refresh_token"
req.params["subject_token_type"] = OAuth2TokenTypeIdentifiers.refreshToken
req.add(params: params)

return req
Expand Down Expand Up @@ -512,12 +513,12 @@ open class OAuth2: OAuth2Base {
}

let req = OAuth2AuthRequest(url: (clientConfig.tokenURL ?? clientConfig.authorizeURL))
req.params["grant_type"] = "urn:ietf:params:oauth:grant-type:token-exchange"
req.params["grant_type"] = OAuth2GrantTypes.tokenExchange
req.params["resource"] = resourceUrl.appendingPathComponent(resourcePath).absoluteString
req.params["scope"] = clientConfig.scope
req.params["requested_token_type"] = "urn:ietf:params:oauth:token-type:access_token"
req.params["requested_token_type"] = OAuth2TokenTypeIdentifiers.accessToken
req.params["subject_token"] = accessToken
req.params["subject_token_type"] = "urn:ietf:params:oauth:token-type:access_token"
req.params["subject_token_type"] = OAuth2TokenTypeIdentifiers.accessToken
req.add(params: params)

return req
Expand Down
3 changes: 2 additions & 1 deletion Sources/Flows/OAuth2ClientCredentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#endif


Expand All @@ -30,7 +31,7 @@ Class to handle two-legged OAuth2 requests of the "client_credentials" type.
open class OAuth2ClientCredentials: OAuth2 {

override open class var grantType: String {
return "client_credentials"
return OAuth2GrantTypes.clientCredentials
}

override open func doAuthorize(params inParams: OAuth2StringDict? = nil) {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Flows/OAuth2CodeGrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#endif


Expand All @@ -34,11 +35,11 @@ key will be embedded into the request body.
open class OAuth2CodeGrant: OAuth2 {

override open class var grantType: String {
return "authorization_code"
return OAuth2GrantTypes.authorizationCode
}

override open class var responseType: String? {
return "code"
return OAuth2ResponseTypes.code
}


Expand Down
3 changes: 2 additions & 1 deletion Sources/Flows/OAuth2DeviceGrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#endif

/// https://www.ietf.org/rfc/rfc8628.html
open class OAuth2DeviceGrant: OAuth2 {
override open class var grantType: String {
return "urn:ietf:params:oauth:grant-type:device_code"
return OAuth2GrantTypes.deviceCode
}

override open class var responseType: String? {
Expand Down
5 changes: 3 additions & 2 deletions Sources/Flows/OAuth2ImplicitGrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#endif


Expand All @@ -30,11 +31,11 @@ Class to handle OAuth2 requests for public clients, such as distributed Mac/iOS
open class OAuth2ImplicitGrant: OAuth2 {

override open class var grantType: String {
return "implicit"
return OAuth2GrantTypes.implicit
}

override open class var responseType: String? {
return "token"
return OAuth2ResponseTypes.token
}

override open func handleRedirectURL(_ redirect: URL) {
Expand Down
3 changes: 2 additions & 1 deletion Sources/Flows/OAuth2PasswordGrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Foundation
#if !NO_MODULE_IMPORT
import Base
import Constants
#if os(macOS)
import macOS
#elseif os(iOS) || os(visionOS)
Expand Down Expand Up @@ -53,7 +54,7 @@ If no credentials are set when authorizing, a native controller is shown so that
open class OAuth2PasswordGrant: OAuth2 {

override open class var grantType: String {
return "password"
return OAuth2GrantTypes.password
}

override open class var clientIdMandatory: Bool {
Expand Down

0 comments on commit d496a44

Please sign in to comment.