Skip to content

Commit ae5b1ea

Browse files
committed
Add enumeration for oauth-token-identification
- Added conversion documentation. - Change conformance for OAuthAccessToken to Codable and CustomStringConvertible.
1 parent 685acf0 commit ae5b1ea

3 files changed

Lines changed: 98 additions & 3 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Conversion for `oauth-types/src/oauth-token-identification.ts`
2+
3+
## `export const oauthTokenIdentificationSchema`
4+
5+
This has been converted.
6+
7+
Swift equivalent:
8+
9+
```swift
10+
public struct TokenIdentification: Codable
11+
```

Sources/OAuthTypes/OAuthAccessToken.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,33 @@
66
//
77

88
/// A structure representing an OAuth access token.
9-
public struct OAuthAccessToken: RawRepresentable {
9+
public struct OAuthAccessToken: Codable, CustomStringConvertible {
1010

11-
public let rawValue: String
11+
private let rawValue: String
1212

13-
public init?(rawValue: String) {
13+
public var description: String {
14+
return rawValue
15+
}
16+
17+
public init?(validating rawValue: String) {
1418
self.rawValue = rawValue
1519
}
20+
21+
public init(from decoder: any Decoder) throws {
22+
let container = try decoder.singleValueContainer()
23+
let value = try container.decode(String.self)
24+
25+
guard let instance = Self.init(validating: value) else {
26+
throw DecodingError.dataCorruptedError(
27+
in: container,
28+
debugDescription: "Invalid OAuthAccessToken value: \(value)"
29+
)
30+
}
31+
self = instance
32+
}
33+
34+
public func encode(to encoder: any Encoder) throws {
35+
var container = encoder.singleValueContainer()
36+
try container.encode(rawValue)
37+
}
1638
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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

Comments
 (0)