From c0798f7f9cc4f6b86f763f5484de563b21b18088 Mon Sep 17 00:00:00 2001 From: Gil Shapira Date: Sun, 17 Nov 2024 17:33:37 +0200 Subject: [PATCH] Update API --- src/docs/DescopeKit.docc/Documentation.md | 1 + src/internal/routes/Auth.swift | 8 +------- src/internal/routes/Shared.swift | 2 +- src/sdk/Callbacks.swift | 14 ++++++++++++-- src/sdk/Routes.swift | 12 +++++++++++- src/types/Responses.swift | 2 +- src/types/Tenant.swift | 11 ++++++++++- src/types/User.swift | 1 - 8 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/docs/DescopeKit.docc/Documentation.md b/src/docs/DescopeKit.docc/Documentation.md index 7a177e2..23be10a 100644 --- a/src/docs/DescopeKit.docc/Documentation.md +++ b/src/docs/DescopeKit.docc/Documentation.md @@ -73,6 +73,7 @@ on the [Descope website](https://descope.com). ### Others - ``DescopeSDK`` +- ``DescopeTenant`` - ``DescopeToken`` - ``DescopeSessionLifecycle`` - ``DescopeSessionStorage`` diff --git a/src/internal/routes/Auth.swift b/src/internal/routes/Auth.swift index 2126984..7713e64 100644 --- a/src/internal/routes/Auth.swift +++ b/src/internal/routes/Auth.swift @@ -10,13 +10,7 @@ final class Auth: DescopeAuth { return try await client.me(refreshJwt: refreshJwt).convert() } - func tenants(by request: TenantsRequest, refreshJwt: String) async throws -> [DescopeTenant] { - var dct = false - var tenantIds: [String] = [] - switch request { - case .selected: dct = true - case .tenantIds(let ids): tenantIds = ids - } + func tenants(dct: Bool, tenantIds: [String], refreshJwt: String) async throws -> [DescopeTenant] { return try await client.tenants(dct: dct, tenantIds: tenantIds, refreshJwt: refreshJwt).convert() } diff --git a/src/internal/routes/Shared.swift b/src/internal/routes/Shared.swift index 26d6885..cfdffc8 100644 --- a/src/internal/routes/Shared.swift +++ b/src/internal/routes/Shared.swift @@ -63,7 +63,7 @@ extension DescopeClient.JWTResponse { guard let sessionJwt, !sessionJwt.isEmpty else { throw DescopeError.decodeError.with(message: "Missing session JWT") } guard let refreshJwt, !refreshJwt.isEmpty else { throw DescopeError.decodeError.with(message: "Missing refresh JWT") } guard let user else { throw DescopeError.decodeError.with(message: "Missing user details") } - return try AuthenticationResponse(sessionToken: Token(jwt: sessionJwt), refreshToken: Token(jwt: refreshJwt), isFirstAuthentication: firstSeen, user: user.convert()) + return try AuthenticationResponse(sessionToken: Token(jwt: sessionJwt), refreshToken: Token(jwt: refreshJwt), user: user.convert(), isFirstAuthentication: firstSeen) } func convert() throws -> RefreshResponse { diff --git a/src/sdk/Callbacks.swift b/src/sdk/Callbacks.swift index 73c736b..78da13f 100644 --- a/src/sdk/Callbacks.swift +++ b/src/sdk/Callbacks.swift @@ -43,10 +43,20 @@ public extension DescopeAuth { } } - func tenants(by request: TenantsRequest, refreshJwt: String, completion: @escaping @Sendable (Result<[DescopeTenant], Error>) -> Void) { + /// Returns the current session user tenants. + /// + /// - Parameters: + /// - dct: Set this to `true` and leave `tenantIds` empty to request the current + /// tenant for the user as set in the `dct` claim. This will fail if a tenant + /// hasn't already been selected. + /// - tenantIds: Provide a non-empty array of tenant IDs and set `dct` to `false` + /// to request a specific list of tenants for the user. + /// + /// - Returns: A list of one or more ``DescopeTenant`` values. + func tenants(dct: Bool, tenantIds: [String], refreshJwt: String, completion: @escaping @Sendable (Result<[DescopeTenant], Error>) -> Void) { Task { do { - completion(.success(try await tenants(by: request, refreshJwt: refreshJwt))) + completion(.success(try await tenants(dct: dct, tenantIds: tenantIds, refreshJwt: refreshJwt))) } catch { completion(.failure(error)) } diff --git a/src/sdk/Routes.swift b/src/sdk/Routes.swift index 0a1fc87..9fafd17 100644 --- a/src/sdk/Routes.swift +++ b/src/sdk/Routes.swift @@ -10,7 +10,17 @@ public protocol DescopeAuth: Sendable { /// - Returns: A ``DescopeUser`` object with the user details. func me(refreshJwt: String) async throws -> DescopeUser - func tenants(by request: TenantsRequest, refreshJwt: String) async throws -> [DescopeTenant] + /// Returns the current session user tenants. + /// + /// - Parameters: + /// - dct: Set this to `true` and leave `tenantIds` empty to request the current + /// tenant for the user as set in the `dct` claim. This will fail if a tenant + /// hasn't already been selected. + /// - tenantIds: Provide a non-empty array of tenant IDs and set `dct` to `false` + /// to request a specific list of tenants for the user. + /// + /// - Returns: A list of one or more ``DescopeTenant`` values. + func tenants(dct: Bool, tenantIds: [String], refreshJwt: String) async throws -> [DescopeTenant] /// Refreshes a ``DescopeSession``. /// diff --git a/src/types/Responses.swift b/src/types/Responses.swift index d7ccae2..321b578 100644 --- a/src/types/Responses.swift +++ b/src/types/Responses.swift @@ -8,8 +8,8 @@ import UIKit public struct AuthenticationResponse: Sendable { public var sessionToken: DescopeToken public var refreshToken: DescopeToken - public var isFirstAuthentication: Bool public var user: DescopeUser + public var isFirstAuthentication: Bool } /// Returned from the ``DescopeAuth/refreshSession(refreshJwt:)`` call. diff --git a/src/types/Tenant.swift b/src/types/Tenant.swift index 791941e..ed48458 100644 --- a/src/types/Tenant.swift +++ b/src/types/Tenant.swift @@ -1,12 +1,21 @@ import Foundation +/// The ``DescopeTenant`` struct represents a tenant in Descope. +/// +/// You can retrieve the tenants for a user after authentication by calling ``DescopeAuth/tenants(dct:tenantIds:refreshJwt:)``. public struct DescopeTenant: @unchecked Sendable { - + /// The unique identifier for the user in the project. + /// + /// This is either an automatically generated value or a custom value that was set + /// when the tenant was created. public var tenantId: String + /// The name of the tenant. public var name: String + /// A mapping of any custom attributes associated with this tenant. The custom attributes + /// are managed via the Descope console. public var customAttributes: [String: Any] public init(tenantId: String, name: String, customAttributes: [String: Any] = [:]) { diff --git a/src/types/User.swift b/src/types/User.swift index f58ee39..5f9ffa0 100644 --- a/src/types/User.swift +++ b/src/types/User.swift @@ -29,7 +29,6 @@ import Foundation /// session manager. If so we ask the Descope server for the latest user details and /// then update the ``DescopeSession`` with them. public struct DescopeUser: @unchecked Sendable { - /// The unique identifier for the user in Descope. /// /// This value never changes after the user is created, and it always matches