Skip to content
This repository has been archived by the owner on Oct 29, 2024. It is now read-only.

Commit

Permalink
Refactor DiscoveryDocument and OAuthDiscoveryDocument structs (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsii777 committed Jan 20, 2024
1 parent 6994801 commit 68d1f10
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 125 deletions.
122 changes: 57 additions & 65 deletions Sources/VaporOAuth/Models/OAuthDiscoveryDocument.swift
Original file line number Diff line number Diff line change
@@ -1,70 +1,46 @@
import Vapor

public struct OAuthDiscoveryDocument: Content {
public let issuer: String
public let authorizationEndpoint: String
public let tokenEndpoint: String
public let userInfoEndpoint: String
public let revocationEndpoint: String
public let introspectionEndpoint: String
public let jwksURI: String
public let registrationEndpoint: String
public let scopesSupported: [String]
public let responseTypesSupported: [String]
public let grantTypesSupported: [String]
public let tokenEndpointAuthMethodsSupported: [String]
public let tokenEndpointAuthSigningAlgValuesSupported: [String]
public let serviceDocumentation: String
public let uiLocalesSupported: [String]
public let opPolicyURI: String
public let opTosURI: String
public let subjectTypesSupported: [String]
public let claimsSupported: [String]


public var issuer: String?
public var authorizationEndpoint: String?
public var tokenEndpoint: String?
public var userInfoEndpoint: String?
public var revocationEndpoint: String?
public var introspectionEndpoint: String?
public var jwksURI: String?
public var registrationEndpoint: String?
public var scopesSupported: [String]?
public var responseTypesSupported: [String]?
public var responseModesSupported: [String]?
public var grantTypesSupported: [String]?
public var acrValuesSupported: [String]?
public var idTokenEncryptionAlgValuesSupported: [String]?
public var idTokenEncryptionEncValuesSupported: [String]?
public var userinfoSigningAlgValuesSupported: [String]?
public var userinfoEncryptionAlgValuesSupported: [String]?
public var userinfoEncryptionEncValuesSupported: [String]?
public var requestObjectSigningAlgValuesSupported: [String]?
public var requestObjectEncryptionAlgValuesSupported: [String]?
public var requestObjectEncryptionEncValuesSupported: [String]?
public var tokenEndpointAuthMethodsSupported: [String]?
public var tokenEndpointAuthSigningAlgValuesSupported: [String]?
public var displayValuesSupported: [String]?
public var claimTypesSupported: [String]?
public var claimsSupported: [String]?
public var serviceDocumentation: String?
public var claimsLocalesSupported: [String]?
public var uiLocalesSupported: [String]?
public var claimsParameterSupported: Bool?
public var requestParameterSupported: Bool?
public var requestUriParameterSupported: Bool?
public var requireRequestUriRegistration: Bool?
public var opPolicyURI: String?
public var opTosURI: String?
public var extend: [String: Any] = [:]

public init(
issuer: String,
authorizationEndpoint: String,
tokenEndpoint: String,
userInfoEndpoint: String,
revocationEndpoint: String,
introspectionEndpoint: String,
jwksURI: String,
registrationEndpoint: String,
scopesSupported: [String],
responseTypesSupported: [String],
grantTypesSupported: [String],
tokenEndpointAuthMethodsSupported: [String],
tokenEndpointAuthSigningAlgValuesSupported: [String],
serviceDocumentation: String,
uiLocalesSupported: [String],
opPolicyURI: String,
opTosURI: String,
subjectTypesSupported: [String],
claimsSupported: [String]
) {
self.issuer = issuer
self.authorizationEndpoint = authorizationEndpoint
self.tokenEndpoint = tokenEndpoint
self.userInfoEndpoint = userInfoEndpoint
self.revocationEndpoint = revocationEndpoint
self.introspectionEndpoint = introspectionEndpoint
self.jwksURI = jwksURI
self.registrationEndpoint = registrationEndpoint
self.scopesSupported = scopesSupported
self.responseTypesSupported = responseTypesSupported
self.grantTypesSupported = grantTypesSupported
self.tokenEndpointAuthMethodsSupported = tokenEndpointAuthMethodsSupported
self.tokenEndpointAuthSigningAlgValuesSupported = tokenEndpointAuthSigningAlgValuesSupported
self.serviceDocumentation = serviceDocumentation
self.uiLocalesSupported = uiLocalesSupported
self.opPolicyURI = opPolicyURI
self.opTosURI = opTosURI
self.subjectTypesSupported = subjectTypesSupported
self.claimsSupported = claimsSupported
}




// Exclude 'extend' property from encoding
private enum CodingKeys: String, CodingKey {
case issuer
Expand All @@ -77,14 +53,30 @@ public struct OAuthDiscoveryDocument: Content {
case registrationEndpoint
case scopesSupported
case responseTypesSupported
case responseModesSupported
case grantTypesSupported
case acrValuesSupported
case idTokenEncryptionAlgValuesSupported
case idTokenEncryptionEncValuesSupported
case userinfoSigningAlgValuesSupported
case userinfoEncryptionAlgValuesSupported
case userinfoEncryptionEncValuesSupported
case requestObjectSigningAlgValuesSupported
case requestObjectEncryptionAlgValuesSupported
case requestObjectEncryptionEncValuesSupported
case tokenEndpointAuthMethodsSupported
case tokenEndpointAuthSigningAlgValuesSupported
case displayValuesSupported
case claimTypesSupported
case claimsSupported
case serviceDocumentation
case claimsLocalesSupported
case uiLocalesSupported
case claimsParameterSupported
case requestParameterSupported
case requestUriParameterSupported
case requireRequestUriRegistration
case opPolicyURI
case opTosURI
case subjectTypesSupported
case claimsSupported
}
}
38 changes: 27 additions & 11 deletions Sources/VaporOAuth/Protocols/DiscoveryDocument.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import Foundation

public protocol DiscoveryDocument: Sendable {
var issuer: String { get }
var authorizationEndpoint: String { get }
var tokenEndpoint: String { get }
var issuer: String? { get }
var authorizationEndpoint: String? { get }
var tokenEndpoint: String? { get }
var userInfoEndpoint: String? { get }
var revocationEndpoint: String { get }
var introspectionEndpoint: String { get }
var jwksURI: String { get }
var revocationEndpoint: String? { get }
var introspectionEndpoint: String? { get }
var jwksURI: String? { get }
var registrationEndpoint: String? { get }
var scopesSupported: [String]? { get }
var responseTypesSupported: [String] { get }
var responseTypesSupported: [String]? { get }
var responseModesSupported: [String]? { get }
var grantTypesSupported: [String]? { get }
var acrValuesSupported: [String]? { get }
var idTokenEncryptionAlgValuesSupported: [String]? { get }
var idTokenEncryptionEncValuesSupported: [String]? { get }
var userinfoSigningAlgValuesSupported: [String]? { get }
var userinfoEncryptionAlgValuesSupported: [String]? { get }
var userinfoEncryptionEncValuesSupported: [String]? { get }
var requestObjectSigningAlgValuesSupported: [String]? { get }
var requestObjectEncryptionAlgValuesSupported: [String]? { get }
var requestObjectEncryptionEncValuesSupported: [String]? { get }
var tokenEndpointAuthMethodsSupported: [String]? { get }
var tokenEndpointAuthSigningAlgValuesSupported: [String]? { get }
var displayValuesSupported: [String]? { get }
var claimTypesSupported: [String]? { get }
var claimsSupported: [String]? { get }
var serviceDocumentation: String? { get }
var claimsLocalesSupported: [String]? { get }
var uiLocalesSupported: [String]? { get }
var claimsParameterSupported: Bool? { get }
var requestParameterSupported: Bool? { get }
var requestUriParameterSupported: Bool? { get }
var requireRequestUriRegistration: Bool? { get }
var opPolicyURI: String? { get }
var opTosURI: String? { get }
var extend: [String: Any] { get set }
var resourceServerRetriever: ResourceServerRetriever? { get }
var subjectTypesSupported: [String] { get }
var claimsSupported: [String]? { get }
// Additional optional claims can be added here following the same pattern.
}
var subjectTypesSupported: [String]? { get } // Made optional to align with OAuthDiscoveryDocument
}
86 changes: 37 additions & 49 deletions Sources/VaporOAuth/RouteHandlers/DiscoveryDocumentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,53 @@ import Vapor

struct DiscoveryDocumentHandler {

let discoveryDocument: DiscoveryDocument // Accept a DiscoveryDocument conforming object
let discoveryDocument: DiscoveryDocument

init(discoveryDocument: DiscoveryDocument) {
self.discoveryDocument = discoveryDocument
}


func generateDiscoveryDocument() -> OAuthDiscoveryDocument {
// Access properties and methods from the provided discoveryDocument
let issuer = discoveryDocument.issuer
let authorizationEndpoint = discoveryDocument.authorizationEndpoint
let tokenEndpoint = discoveryDocument.tokenEndpoint
let userInfoEndpoint = discoveryDocument.userInfoEndpoint
let revocationEndpoint = discoveryDocument.revocationEndpoint
let introspectionEndpoint = discoveryDocument.introspectionEndpoint
let jwksURI = discoveryDocument.jwksURI
let registrationEndpoint = discoveryDocument.registrationEndpoint
let scopesSupported = discoveryDocument.scopesSupported
let responseTypesSupported = discoveryDocument.responseTypesSupported
let grantTypesSupported = discoveryDocument.grantTypesSupported
let tokenEndpointAuthMethodsSupported = discoveryDocument.tokenEndpointAuthMethodsSupported
let tokenEndpointAuthSigningAlgValuesSupported = discoveryDocument.tokenEndpointAuthSigningAlgValuesSupported
let serviceDocumentation = discoveryDocument.serviceDocumentation
let uiLocalesSupported = discoveryDocument.uiLocalesSupported
let opPolicyURI = discoveryDocument.opPolicyURI
let opTosURI = discoveryDocument.opTosURI
let subjectTypesSupported = discoveryDocument.subjectTypesSupported
let claimsSupported = discoveryDocument.claimsSupported

// Create an OAuthDiscoveryDocument object
let discoveryDocument = OAuthDiscoveryDocument(
issuer: issuer,
authorizationEndpoint: authorizationEndpoint,
tokenEndpoint: tokenEndpoint,
userInfoEndpoint: userInfoEndpoint,
revocationEndpoint: revocationEndpoint,
introspectionEndpoint: introspectionEndpoint,
jwksURI: jwksURI,
registrationEndpoint: registrationEndpoint,
scopesSupported: scopesSupported,
responseTypesSupported: responseTypesSupported,
grantTypesSupported: grantTypesSupported,
tokenEndpointAuthMethodsSupported: tokenEndpointAuthMethodsSupported,
tokenEndpointAuthSigningAlgValuesSupported: tokenEndpointAuthSigningAlgValuesSupported,
serviceDocumentation: serviceDocumentation,
uiLocalesSupported: uiLocalesSupported,
opPolicyURI: opPolicyURI,
opTosURI: opTosURI,
subjectTypesSupported: subjectTypesSupported,
claimsSupported: claimsSupported
return OAuthDiscoveryDocument(
issuer: discoveryDocument.issuer,
authorizationEndpoint: discoveryDocument.authorizationEndpoint,
tokenEndpoint: discoveryDocument.tokenEndpoint,
userInfoEndpoint: discoveryDocument.userInfoEndpoint,
revocationEndpoint: discoveryDocument.revocationEndpoint,
introspectionEndpoint: discoveryDocument.introspectionEndpoint,
jwksURI: discoveryDocument.jwksURI,
registrationEndpoint: discoveryDocument.registrationEndpoint,
scopesSupported: discoveryDocument.scopesSupported,
responseTypesSupported: discoveryDocument.responseTypesSupported,
responseModesSupported: discoveryDocument.responseModesSupported,
grantTypesSupported: discoveryDocument.grantTypesSupported,
acrValuesSupported: discoveryDocument.acrValuesSupported,
idTokenEncryptionAlgValuesSupported: discoveryDocument.idTokenEncryptionAlgValuesSupported,
idTokenEncryptionEncValuesSupported: discoveryDocument.idTokenEncryptionEncValuesSupported,
userinfoSigningAlgValuesSupported: discoveryDocument.userinfoSigningAlgValuesSupported,
userinfoEncryptionAlgValuesSupported: discoveryDocument.userinfoEncryptionAlgValuesSupported,
userinfoEncryptionEncValuesSupported: discoveryDocument.userinfoEncryptionEncValuesSupported,
requestObjectSigningAlgValuesSupported: discoveryDocument.requestObjectSigningAlgValuesSupported,
requestObjectEncryptionAlgValuesSupported: discoveryDocument.requestObjectEncryptionAlgValuesSupported,
requestObjectEncryptionEncValuesSupported: discoveryDocument.requestObjectEncryptionEncValuesSupported,
tokenEndpointAuthMethodsSupported: discoveryDocument.tokenEndpointAuthMethodsSupported,
tokenEndpointAuthSigningAlgValuesSupported: discoveryDocument.tokenEndpointAuthSigningAlgValuesSupported,
displayValuesSupported: discoveryDocument.displayValuesSupported,
claimTypesSupported: discoveryDocument.claimTypesSupported,
claimsSupported: discoveryDocument.claimsSupported,
serviceDocumentation: discoveryDocument.serviceDocumentation,
claimsLocalesSupported: discoveryDocument.claimsLocalesSupported,
uiLocalesSupported: discoveryDocument.uiLocalesSupported,
claimsParameterSupported: discoveryDocument.claimsParameterSupported,
requestParameterSupported: discoveryDocument.requestParameterSupported,
requestUriParameterSupported: discoveryDocument.requireRequestUriRegistration,
requireRequestUriRegistration: discoveryDocument.requireRequestUriRegistration,
opPolicyURI: discoveryDocument.opPolicyURI,
opTosURI: discoveryDocument.opTosURI
)

// Return the generated discovery document
return discoveryDocument

}

func handleRequest(request: Request) throws -> OAuthDiscoveryDocument {
// Generate and return the OAuth 2.0 Discovery Document
return generateDiscoveryDocument()
}
}

0 comments on commit 68d1f10

Please sign in to comment.