Skip to content

Commit

Permalink
Merge pull request #46 from niscy-eudiw/feature/remove-self-signed
Browse files Browse the repository at this point in the history
[fix] removed self signed
  • Loading branch information
dtsiflit authored Jun 28, 2024
2 parents 5602be6 + d654eda commit fa4f7a6
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 131 deletions.
5 changes: 1 addition & 4 deletions Sources/Entities/Wallet/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ public struct OpenId4VCIConfig {
public let clientId: ClientId
public let authFlowRedirectionURI: URL
public let authorizeIssuanceConfig: AuthorizeIssuanceConfig
public let usesSelfSignedDelegation: Bool

public init(
clientId: ClientId,
authFlowRedirectionURI: URL,
authorizeIssuanceConfig: AuthorizeIssuanceConfig = .favorScopes,
usesSelfSignedDelegation: Bool = false
authorizeIssuanceConfig: AuthorizeIssuanceConfig = .favorScopes
) {
self.clientId = clientId
self.authFlowRedirectionURI = authFlowRedirectionURI
self.authorizeIssuanceConfig = authorizeIssuanceConfig
self.usesSelfSignedDelegation = usesSelfSignedDelegation
}
}

20 changes: 5 additions & 15 deletions Sources/Issuers/Issuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,29 @@ public actor Issuer: IssuerType {
self.issuerMetadata = issuerMetadata
self.config = config

var authorizationServerParPoster = parPoster
authorizationServerParPoster.usesSelfSignedDelegation = config.usesSelfSignedDelegation
var authorizationServerTokenPoster = tokenPoster
authorizationServerTokenPoster.usesSelfSignedDelegation = config.usesSelfSignedDelegation
authorizer = try AuthorizationServerClient(
parPoster: authorizationServerParPoster,
tokenPoster: authorizationServerTokenPoster,
parPoster: parPoster,
tokenPoster: tokenPoster,
config: config,
authorizationServerMetadata: authorizationServerMetadata,
credentialIssuerIdentifier: issuerMetadata.credentialIssuerIdentifier,
dpopConstructor: dpopConstructor
)

var issuanceRequesterPoster = requesterPoster
issuanceRequesterPoster.usesSelfSignedDelegation = config.usesSelfSignedDelegation
issuanceRequester = IssuanceRequester(
issuerMetadata: issuerMetadata,
poster: issuanceRequesterPoster,
poster: requesterPoster,
dpopConstructor: dpopConstructor
)

var deferredPoster = deferredRequesterPoster
deferredPoster.usesSelfSignedDelegation = config.usesSelfSignedDelegation
deferredIssuanceRequester = IssuanceRequester(
issuerMetadata: issuerMetadata,
poster: deferredPoster
poster: deferredRequesterPoster
)

var notifyIssuerPoster = notificationPoster
notifyIssuerPoster.usesSelfSignedDelegation = config.usesSelfSignedDelegation
notifyIssuer = NotifyIssuer(
issuerMetadata: issuerMetadata,
poster: notifyIssuerPoster
poster: notificationPoster
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,11 @@ public actor AuthorizationServerMetadataResolver: AuthorizationServerMetadataRes
private let oauthFetcher: Fetcher<AuthorizationServerMetadata>

public init(
usesSelfSignedDelegation: Bool = false,
oidcFetcher: Fetcher<OIDCProviderMetadata> = Fetcher(),
oauthFetcher: Fetcher<AuthorizationServerMetadata> = Fetcher()
) {
var oidc = oidcFetcher
oidc.usesSelfSignedDelegation = usesSelfSignedDelegation
self.oidcFetcher = oidc

var oauth = oauthFetcher
oauth.usesSelfSignedDelegation = usesSelfSignedDelegation
self.oauthFetcher = oauth
self.oidcFetcher = oidcFetcher
self.oauthFetcher = oauthFetcher
}

/// Resolves client metadata asynchronously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ public actor CredentialIssuerMetadataResolver: CredentialIssuerMetadataType {
private let fetcher: Fetcher<CredentialIssuerMetadata>

public init(
usesSelfSignedDelegation: Bool = false,
fetcher: Fetcher<CredentialIssuerMetadata> = Fetcher()
) {
var metaDataFetcher = fetcher
metaDataFetcher.usesSelfSignedDelegation = usesSelfSignedDelegation
self.fetcher = metaDataFetcher
self.fetcher = fetcher
}

/// Resolves client metadata asynchronously.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,11 @@ public actor CredentialOfferRequestResolver {
/// - credentialIssuerMetadataResolver: An object responsible for resolving credential issuer metadata.
/// - authorizationServerMetadataResolver: An object responsible for resolving authorization server metadata.
public init(
usesSelfSignedDelegation: Bool = false,
fetcher: Fetcher<CredentialOfferRequestObject> = Fetcher(),
credentialIssuerMetadataResolver: CredentialIssuerMetadataResolver = CredentialIssuerMetadataResolver(),
authorizationServerMetadataResolver: AuthorizationServerMetadataResolver = AuthorizationServerMetadataResolver()
) {
var credentialOfferFetcher = fetcher
credentialOfferFetcher.usesSelfSignedDelegation = usesSelfSignedDelegation
self.fetcher = credentialOfferFetcher

self.fetcher = fetcher
self.credentialIssuerMetadataResolver = credentialIssuerMetadataResolver
self.authorizationServerMetadataResolver = authorizationServerMetadataResolver
}
Expand Down
29 changes: 4 additions & 25 deletions Sources/Utilities/RemoteDataAccess/Fetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public enum FetchError: LocalizedError {

public protocol Fetching {
var session: Networking { get set }
var usesSelfSignedDelegation: Bool { get set }

associatedtype Element: Decodable

Expand All @@ -64,16 +63,14 @@ public protocol Fetching {
public struct Fetcher<Element: Decodable>: Fetching {

public var session: Networking
public var usesSelfSignedDelegation: Bool

/**
Initializes a Fetcher instance.
*/
public init(
session: Networking = URLSession.shared,
usesSelfSignedDelegation: Bool = false
session: Networking = URLSession.shared
) {
self.session = session
self.usesSelfSignedDelegation = usesSelfSignedDelegation
}

/**
Expand All @@ -86,16 +83,7 @@ public struct Fetcher<Element: Decodable>: Fetching {
*/
public func fetch(url: URL) async -> Result<Element, FetchError> {
do {
let session: Networking = {
if self.usesSelfSignedDelegation {
let delegate = SelfSignedSessionDelegate()
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
} else {
return self.session
}
}()
let (data, response) = try await session.data(from: url)
let (data, response) = try await self.session.data(from: url)

let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0
if !statusCode.isWithinRange(200...299) {
Expand All @@ -117,16 +105,7 @@ public struct Fetcher<Element: Decodable>: Fetching {

public func fetchString(url: URL) async throws -> Result<String, FetchError> {
do {
let session: Networking = {
if self.usesSelfSignedDelegation {
let delegate = SelfSignedSessionDelegate()
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
} else {
return self.session
}
}()
let (data, response) = try await session.data(from: url)
let (data, response) = try await self.session.data(from: url)

let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0
if !statusCode.isWithinRange(200...299) {
Expand Down
28 changes: 3 additions & 25 deletions Sources/Utilities/RemoteDataAccess/Poster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public enum PostError: LocalizedError {
public protocol PostingType {

var session: Networking { get set }
var usesSelfSignedDelegation: Bool { get set }

/**
Performs a POST request with the provided URLRequest.
Expand All @@ -72,17 +71,14 @@ public protocol PostingType {
public struct Poster: PostingType {

public var session: Networking
public var usesSelfSignedDelegation: Bool

/**
Initializes a Poster instance.
*/
public init(
session: Networking = URLSession.shared,
usesSelfSignedDelegation: Bool = false
session: Networking = URLSession.shared
) {
self.session = session
self.usesSelfSignedDelegation = usesSelfSignedDelegation
}

/**
Expand All @@ -95,16 +91,7 @@ public struct Poster: PostingType {
*/
public func post<Response: Codable>(request: URLRequest) async -> Result<Response, PostError> {
do {
let session: Networking = {
if self.usesSelfSignedDelegation {
let delegate = SelfSignedSessionDelegate()
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
} else {
return self.session
}
}()
let (data, response) = try await session.data(for: request)
let (data, response) = try await self.session.data(for: request)
let statusCode = (response as? HTTPURLResponse)?.statusCode ?? 0

if statusCode >= 400 && statusCode < 500 {
Expand Down Expand Up @@ -143,16 +130,7 @@ public struct Poster: PostingType {
*/
public func check(request: URLRequest) async -> Result<Bool, PostError> {
do {
let session: Networking = {
if self.usesSelfSignedDelegation {
let delegate = SelfSignedSessionDelegate()
let configuration = URLSessionConfiguration.default
return URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil)
} else {
return self.session
}
}()
let (_, response) = try await session.data(for: request)
let (_, response) = try await self.session.data(for: request)

return .success((response as? HTTPURLResponse)?.statusCode.isWithinRange(200...299) ?? false)
} catch let error as NSError {
Expand Down
40 changes: 5 additions & 35 deletions Tests/Helpers/Wallet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ extension Wallet {
let credentialConfigurationIdentifier = try CredentialConfigurationIdentifier(value: identifier)
let credentialIssuerIdentifier = try CredentialIssuerId(CREDENTIAL_ISSUER_PUBLIC_URL)

let resolver = CredentialIssuerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
let resolver = CredentialIssuerMetadataResolver()
let issuerMetadata = await resolver.resolve(
source: .credentialIssuer(
credentialIssuerIdentifier
Expand All @@ -44,9 +42,7 @@ extension Wallet {
case .success(let metaData):
if let authorizationServer = metaData?.authorizationServers.first,
let metaData {
let resolver = AuthorizationServerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
let resolver = AuthorizationServerMetadataResolver()
let authServerMetadata = await resolver.resolve(url: authorizationServer)

let offer = try CredentialOffer(
Expand Down Expand Up @@ -171,15 +167,7 @@ extension Wallet {
offerUri: String,
claimSet: ClaimSet? = nil
) async throws -> [(String, String)] {
let resolver = CredentialOfferRequestResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation,
credentialIssuerMetadataResolver: CredentialIssuerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
),
authorizationServerMetadataResolver: AuthorizationServerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
)
let resolver = CredentialOfferRequestResolver()
let result = await resolver
.resolve(
source: try .init(
Expand All @@ -203,16 +191,7 @@ extension Wallet {
scope: String,
claimSet: ClaimSet? = nil
) async throws -> String {
let result = await CredentialOfferRequestResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation,
credentialIssuerMetadataResolver: CredentialIssuerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
),
authorizationServerMetadataResolver:
AuthorizationServerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
).resolve(
let result = await CredentialOfferRequestResolver().resolve(
source: try .init(
urlString: offerUri
)
Expand All @@ -235,16 +214,7 @@ extension Wallet {
scope: String,
claimSet: ClaimSet? = nil
) async throws -> String {
let result = await CredentialOfferRequestResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation,
credentialIssuerMetadataResolver: CredentialIssuerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
),
authorizationServerMetadataResolver:
AuthorizationServerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
).resolve(
let result = await CredentialOfferRequestResolver().resolve(
source: try .init(
urlString: offerUri
)
Expand Down
11 changes: 1 addition & 10 deletions Tests/Issuance/IssuanceBatchRequestTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,7 @@ class IssuanceBatchRequestTest: XCTestCase {
// Given
let url = "\(CREDENTIAL_ISSUER_PUBLIC_URL)/credentialoffer?credential_offer=\(SdJwtVC_CredentialOffer)"

guard let offer = try? await CredentialOfferRequestResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation,
credentialIssuerMetadataResolver: CredentialIssuerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
),
authorizationServerMetadataResolver:
AuthorizationServerMetadataResolver(
usesSelfSignedDelegation: config.usesSelfSignedDelegation
)
).resolve(
guard let offer = try? await CredentialOfferRequestResolver().resolve(
source: try .init(
urlString: url
)
Expand Down

0 comments on commit fa4f7a6

Please sign in to comment.