Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : MacLustr Identity9// File : sdk/swift/Sources/MacLustrIdentityCore/IdentitySession.swift10// Module : MacLustrIdentityCore11// Repository : https://git.spboucher.ai/maclustr-identity12// Project Website : https://identity.spboucher.ai13// Created : 2026-12-0114// Last Modified : 2026-12-0115//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author : Simon-Pierre Boucher21// Position : Professor of Finance22// Institution : Université du Québec en Outaouais (UQO)23// Department : Département des sciences administratives24// Organization : SP Boucher25//26// Website : https://spboucher.ai27// Git : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General : info@spboucher.ai34// Professional : contact@spboucher.ai35// Direct : simon@spboucher.ai36// Research : research@spboucher.ai37// Development : dev@spboucher.ai38// Support : support@spboucher.ai39// Security/Admin : admin@spboucher.ai40// Notifications : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification : Public67// Distribution : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License : None specified (no license file in this repository)81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788// IdentitySession: what the integrating app receives from its own backend.89import Foundation9091/// A verification session handed to the app by its own backend.92///93/// The app never holds a secret API key. Its backend creates the session with94/// `POST /v1/verification-sessions` and returns either `{session_id, client_secret}`95/// or the hosted `url` (`https://identity.spboucher.ai/verify/{id}#{client_secret}`).96public struct IdentitySession: Equatable, Sendable {97 /// Origin of the MacLustr Identity service, without a path (for example `https://identity.spboucher.ai`).98 public let baseURL: URL99 public let id: String100 /// Session-scoped secret (`vsc_…`). Valid for this session only, until it expires.101 public let clientSecret: String102103 /// Production origin of MacLustr Identity.104 public static let defaultBaseURL = URL(string: "https://identity.spboucher.ai")!105106 public init(baseURL: URL = IdentitySession.defaultBaseURL, sessionID: String, clientSecret: String) throws {107 guard let origin = IdentitySession.origin(of: baseURL) else { throw IdentityError.invalidSessionURL }108 guard IdentitySession.isValidID(sessionID), IdentitySession.isValidSecret(clientSecret) else { throw IdentityError.invalidSessionURL }109 self.baseURL = origin110 self.id = sessionID111 self.clientSecret = clientSecret112 }113114 /// Parses the hosted URL returned by the backend: `https://host/verify/{id}#{client_secret}`.115 public init(url: URL) throws {116 guard let comps = URLComponents(url: url, resolvingAgainstBaseURL: false), let secret = comps.fragment else {117 throw IdentityError.invalidSessionURL118 }119 let parts = comps.path.split(separator: "/").map(String.init)120 guard parts.count == 2, parts[0] == "verify" else { throw IdentityError.invalidSessionURL }121 try self.init(baseURL: url, sessionID: parts[1], clientSecret: secret)122 }123124 /// Parses the hosted URL from a string (leading and trailing whitespace is ignored).125 public init(urlString: String) throws {126 guard let url = URL(string: urlString.trimmingCharacters(in: .whitespacesAndNewlines)) else { throw IdentityError.invalidSessionURL }127 try self.init(url: url)128 }129130 /// The hosted web page for this session (used by `IdentityWebFlow`).131 public var hostedURL: URL {132 var comps = URLComponents(url: baseURL, resolvingAgainstBaseURL: false)!133 comps.path = "/verify/\(id)"134 comps.fragment = clientSecret135 return comps.url!136 }137138 /// Keeps scheme, host and port only. Plain http is accepted for loopback hosts only (local development).139 static func origin(of url: URL) -> URL? {140 guard let comps = URLComponents(url: url, resolvingAgainstBaseURL: false), let scheme = comps.scheme?.lowercased(),141 let host = comps.host?.lowercased(), !host.isEmpty, comps.user == nil, comps.password == nil else { return nil }142 let loopback = host == "localhost" || host == "127.0.0.1" || host == "::1" || host == "[::1]"143 guard scheme == "https" || (scheme == "http" && loopback) else { return nil }144 var out = URLComponents()145 out.scheme = scheme146 out.host = comps.host147 out.port = comps.port148 return out.url149 }150151 private static let idCharacters = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")152153 static func isValidID(_ s: String) -> Bool {154 s.hasPrefix("vs_") && s.count > 3 && s.count <= 80 && s.unicodeScalars.allSatisfy(idCharacters.contains)155 }156157 static func isValidSecret(_ s: String) -> Bool {158 s.hasPrefix("vsc_") && s.count > 4 && s.count <= 200 && s.unicodeScalars.allSatisfy(idCharacters.contains)159 }160}161162extension IdentitySession: CustomStringConvertible, CustomDebugStringConvertible {163 /// The client secret is never printed.164 public var description: String { "IdentitySession(\(id) @ \(baseURL.absoluteString))" }165 public var debugDescription: String { description }166}167