Skip to content

Commit

Permalink
Update API
Browse files Browse the repository at this point in the history
  • Loading branch information
shilgapira committed Nov 17, 2024
1 parent c7c649f commit c0798f7
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/docs/DescopeKit.docc/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ on the [Descope website](https://descope.com).
### Others

- ``DescopeSDK``
- ``DescopeTenant``
- ``DescopeToken``
- ``DescopeSessionLifecycle``
- ``DescopeSessionStorage``
Expand Down
8 changes: 1 addition & 7 deletions src/internal/routes/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion src/internal/routes/Shared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions src/sdk/Callbacks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
12 changes: 11 additions & 1 deletion src/sdk/Routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
///
Expand Down
2 changes: 1 addition & 1 deletion src/types/Responses.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/types/Tenant.swift
Original file line number Diff line number Diff line change
@@ -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] = [:]) {
Expand Down
1 change: 0 additions & 1 deletion src/types/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c0798f7

Please sign in to comment.