From 68d1f1023ced7527e5dbc9fa0a6ce82e8046a400 Mon Sep 17 00:00:00 2001 From: Vamsi Madduluri Date: Sat, 20 Jan 2024 23:10:26 +0530 Subject: [PATCH] Refactor DiscoveryDocument and OAuthDiscoveryDocument structs (#14) --- .../Models/OAuthDiscoveryDocument.swift | 122 ++++++++---------- .../Protocols/DiscoveryDocument.swift | 38 ++++-- .../DiscoveryDocumentHandler.swift | 86 ++++++------ 3 files changed, 121 insertions(+), 125 deletions(-) diff --git a/Sources/VaporOAuth/Models/OAuthDiscoveryDocument.swift b/Sources/VaporOAuth/Models/OAuthDiscoveryDocument.swift index d21cb16..a255804 100644 --- a/Sources/VaporOAuth/Models/OAuthDiscoveryDocument.swift +++ b/Sources/VaporOAuth/Models/OAuthDiscoveryDocument.swift @@ -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 @@ -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 } } diff --git a/Sources/VaporOAuth/Protocols/DiscoveryDocument.swift b/Sources/VaporOAuth/Protocols/DiscoveryDocument.swift index 327a9e5..8d89f82 100644 --- a/Sources/VaporOAuth/Protocols/DiscoveryDocument.swift +++ b/Sources/VaporOAuth/Protocols/DiscoveryDocument.swift @@ -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. -} \ No newline at end of file + var subjectTypesSupported: [String]? { get } // Made optional to align with OAuthDiscoveryDocument +} diff --git a/Sources/VaporOAuth/RouteHandlers/DiscoveryDocumentHandler.swift b/Sources/VaporOAuth/RouteHandlers/DiscoveryDocumentHandler.swift index 6026975..2d97671 100644 --- a/Sources/VaporOAuth/RouteHandlers/DiscoveryDocumentHandler.swift +++ b/Sources/VaporOAuth/RouteHandlers/DiscoveryDocumentHandler.swift @@ -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() } }