|
| 1 | +// |
| 2 | +// OAuthTokenIdentification.swift |
| 3 | +// ATOAuthKit |
| 4 | +// |
| 5 | +// Created by Christopher Jr Riley on 2025-08-03. |
| 6 | +// |
| 7 | + |
| 8 | +/// An enumeration represents a request to identify an OAuth token for introspection or |
| 9 | +/// revocation purposes. |
| 10 | +public struct TokenIdentification: Codable { |
| 11 | + |
| 12 | + /// The actual OAuth token string. |
| 13 | + public let token: Token |
| 14 | + |
| 15 | + /// A hint to help the server identify the type of token being sent. |
| 16 | + public let tokenTypeHint: TokenTypeHint |
| 17 | + |
| 18 | + /// A representation of possible OAuth token variants. |
| 19 | + public enum Token: Codable { |
| 20 | + |
| 21 | + /// An OAuth access token. |
| 22 | + case accessToken(OAuthAccessToken) |
| 23 | + |
| 24 | + /// An OAuth refresh token |
| 25 | + case refreshToken(OAuthRefreshToken) |
| 26 | + |
| 27 | + public init(from decoder: any Decoder) throws { |
| 28 | + let container = try decoder.singleValueContainer() |
| 29 | + |
| 30 | + if let value = try? container.decode(OAuthAccessToken.self) { |
| 31 | + self = .accessToken(value) |
| 32 | + } else if let value = try? container.decode(OAuthRefreshToken.self) { |
| 33 | + self = .refreshToken(value) |
| 34 | + } else { |
| 35 | + throw DecodingError.typeMismatch( |
| 36 | + JWT.self, DecodingError.Context( |
| 37 | + codingPath: decoder.codingPath, debugDescription: "Unknown ClientCredentials type")) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public func encode(to encoder: Encoder) throws { |
| 42 | + var container = encoder.singleValueContainer() |
| 43 | + |
| 44 | + switch self { |
| 45 | + case .accessToken(let value): |
| 46 | + try container.encode(value) |
| 47 | + case .refreshToken(let value): |
| 48 | + try container.encode(value) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// A representation of valid hints for token type when identifying an OAuth token. |
| 54 | + public enum TokenTypeHint: String, Codable { |
| 55 | + |
| 56 | + /// Indicates that the provided token is an access token. |
| 57 | + case accessToken = "access_token" |
| 58 | + |
| 59 | + /// Indicates that the provided token is a refresh token. |
| 60 | + case refreshToken = "refresh_token" |
| 61 | + } |
| 62 | +} |
0 commit comments