Skip to content

Commit

Permalink
Ensure SDKVersion registration locks
Browse files Browse the repository at this point in the history
  • Loading branch information
mikenachbaur-okta committed Jul 23, 2024
1 parent 9521d2d commit d9bd306
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
40 changes: 28 additions & 12 deletions Sources/AuthFoundation/Network/Internal/String+AuthFoundation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,47 @@ private let systemVersion: String = {
#endif
}()

public final class SDKVersion {
/// Utility class that allows SDK components to register their name and version for use in HTTP User-Agent values.
///
/// The Okta Client SDK consists of multiple libraries, each of which may or may not be used within the same application, or at the same time. To allow version information to be sustainably managed, this class can be used to centralize the registration of these SDK versions to report just the components used within an application.
public final class SDKVersion: Sendable {
/// The name of this library component.
public let name: String

/// The version number string of this library component.
public let version: String

public init(sdk name: String, version: String) {
self.name = name
self.version = version
}


/// The formatted display name for this individual library's information.
public var displayName: String { "\(name)/\(version)" }

/// The calculated user agent string that will be included in outgoing network requests.
public private(set) static var userAgent: String = ""

private static let lock = UnfairLock()
fileprivate static var sdkVersions: [SDKVersion] = []

/// Register a new SDK library component to be added to the ``userAgent`` value.
/// > Note: SDK ``name`` values must be unique. If a duplicate SDK version is already added, only the first registered SDK value will be applied.
/// - Parameter sdk: SDK version to add.
public static func register(sdk: SDKVersion) {
guard sdkVersions.filter({ $0.name == sdk.name }).isEmpty else {
return
lock.withLock {
guard sdkVersions.filter({ $0.name == sdk.name }).isEmpty else {
return
}

sdkVersions.append(sdk)

let sdkVersions = SDKVersion.sdkVersions
.sorted(by: { $0.name < $1.name })
.map(\.displayName)
.joined(separator: " ")
userAgent = "\(sdkVersions) \(systemName)/\(systemVersion) Device/\(deviceModel)"
}

sdkVersions.append(sdk)

let sdkVersions = SDKVersion.sdkVersions
.sorted(by: { $0.name < $1.name })
.map(\.displayName)
.joined(separator: " ")
userAgent = "\(sdkVersions) \(systemName)/\(systemVersion) Device/\(deviceModel)"
}
}

Expand Down
3 changes: 1 addition & 2 deletions Sources/OktaOAuth2/Authentication/ResourceOwnerFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import AuthFoundation
///
/// This simple authentication flow permits a suer to authenticate using a simple username and password. As such, the configuration is straightforward.
///
/// > Important: Resource Owner authentication does not support MFA or other more secure authentication models, and is not recommended for production applications.
@available(*, deprecated, message: "Please use the DirectAuth SDK's DirectAuthenticationFlow class instead")
/// > Important: Resource Owner authentication does not support MFA or other more secure authentication models, and is not recommended for production applications. Please use the DirectAuth SDK's DirectAuthenticationFlow class instead.
public class ResourceOwnerFlow: AuthenticationFlow {
/// The OAuth2Client this authentication flow will use.
public let client: OAuth2Client
Expand Down

0 comments on commit d9bd306

Please sign in to comment.