|
8 | 8 | import CryptoKit |
9 | 9 | import Foundation |
10 | 10 |
|
11 | | -// class IterableJwtGenerator { |
12 | | - |
13 | | -// private static let algorithm = "HS256" |
14 | | -// private static let maxTokenLifetimeMs: Int64 = 365 * 24 * 60 * 60 * 1000 // 1 year in milliseconds |
15 | | - |
16 | | -// private static func millisToSeconds(_ millis: Int64) -> Int64 { |
17 | | -// return millis / 1000 |
18 | | -// } |
19 | | - |
20 | | -// /// Base64 URL encode without padding |
21 | | -// private static func base64UrlEncode(_ data: Data) -> String { |
22 | | -// let base64 = data.base64EncodedString() |
23 | | -// return |
24 | | -// base64 |
25 | | -// .replacingOccurrences(of: "+", with: "-") |
26 | | -// .replacingOccurrences(of: "/", with: "_") |
27 | | -// .replacingOccurrences(of: "=", with: "") |
28 | | -// } |
29 | | - |
30 | | -// private static let encodedHeader: String = { |
31 | | -// let header = "{\"alg\":\"HS256\",\"typ\":\"JWT\"}" |
32 | | -// let headerData = header.data(using: .utf8)! |
33 | | -// return base64UrlEncode(headerData) |
34 | | -// }() |
35 | | - |
36 | | -// /// Generates a JWT from the provided secret and payload |
37 | | -// /// - Parameters: |
38 | | -// /// - secret: Your organization's shared secret with Iterable |
39 | | -// /// - payload: The JSON payload |
40 | | -// /// - Returns: A signed JWT |
41 | | -// static func generateToken(secret: String, payload: String) throws -> String { |
42 | | -// guard let payloadData = payload.data(using: .utf8) else { |
43 | | -// throw NSError( |
44 | | -// domain: "JWTGenerator", code: 1, userInfo: [NSLocalizedDescriptionKey: "Invalid payload"]) |
45 | | -// } |
46 | | - |
47 | | -// let encodedPayload = base64UrlEncode(payloadData) |
48 | | -// let encodedHeaderAndPayload = "\(encodedHeader).\(encodedPayload)" |
49 | | - |
50 | | -// guard let secretData = secret.data(using: .utf8), |
51 | | -// let messageData = encodedHeaderAndPayload.data(using: .utf8) |
52 | | -// else { |
53 | | -// throw NSError( |
54 | | -// domain: "JWTGenerator", code: 2, |
55 | | -// userInfo: [NSLocalizedDescriptionKey: "Invalid secret or message"]) |
56 | | -// } |
57 | | - |
58 | | -// // HMAC-SHA256 signature |
59 | | -// let key = SymmetricKey(data: secretData) |
60 | | -// let signature = HMAC<SHA256>.authenticationCode(for: messageData, using: key) |
61 | | -// let signatureData = Data(signature) |
62 | | -// let encodedSignature = base64UrlEncode(signatureData) |
63 | | - |
64 | | -// return "\(encodedHeaderAndPayload).\(encodedSignature)" |
65 | | -// } |
66 | | - |
67 | | -// /// Generates a JWT (issued now, expires after the provided duration) |
68 | | -// /// - Parameters: |
69 | | -// /// - secret: Your organization's shared secret with Iterable |
70 | | -// /// - durationMs: The token's expiration time in milliseconds. Up to one year. |
71 | | -// /// - email: The email to include in the token, or nil |
72 | | -// /// - userId: The userId to include in the token, or nil |
73 | | -// /// - Returns: A JWT string |
74 | | -// static func generateToken(secret: String, durationMs: Int64, email: String?, userId: String?) |
75 | | -// throws -> String |
76 | | -// { |
77 | | -// guard durationMs <= maxTokenLifetimeMs else { |
78 | | -// throw NSError( |
79 | | -// domain: "JWTGenerator", code: 3, |
80 | | -// userInfo: [NSLocalizedDescriptionKey: "Duration must be one year or less."]) |
81 | | -// } |
82 | | - |
83 | | -// let hasEmail = email != nil && !email!.isEmpty |
84 | | -// let hasUserId = userId != nil && !userId!.isEmpty |
85 | | - |
86 | | -// guard (hasEmail && !hasUserId) || (!hasEmail && hasUserId) else { |
87 | | -// throw NSError( |
88 | | -// domain: "JWTGenerator", code: 4, |
89 | | -// userInfo: [ |
90 | | -// NSLocalizedDescriptionKey: "The token must include a userId or email, but not both." |
91 | | -// ]) |
92 | | -// } |
93 | | - |
94 | | -// let now = millisToSeconds(Int64(Date().timeIntervalSince1970 * 1000)) |
95 | | -// let exp = now + millisToSeconds(durationMs) |
96 | | - |
97 | | -// var payloadDict: [String: Any] = [ |
98 | | -// "iat": now, |
99 | | -// "exp": exp, |
100 | | -// ] |
101 | | - |
102 | | -// if let userId = userId { |
103 | | -// payloadDict["userId"] = userId |
104 | | -// } else if let email = email { |
105 | | -// payloadDict["email"] = email |
106 | | -// } |
107 | | - |
108 | | -// guard let payloadData = try? JSONSerialization.data(withJSONObject: payloadDict, options: []), |
109 | | -// let payload = String(data: payloadData, encoding: .utf8) |
110 | | -// else { |
111 | | -// throw NSError( |
112 | | -// domain: "JWTGenerator", code: 5, |
113 | | -// userInfo: [NSLocalizedDescriptionKey: "Failed to serialize payload"]) |
114 | | -// } |
115 | | - |
116 | | -// return try generateToken(secret: secret, payload: payload) |
117 | | -// } |
118 | | -// } |
119 | | - |
120 | | -// |
121 | | -// IterableTokenGenerator.swift |
122 | | -// swift-sdk |
123 | | -// |
124 | | -// Created by Apple on 22/10/24. |
125 | | -// Copyright © 2024 Iterable. All rights reserved. |
126 | | -// |
127 | | - |
128 | 11 | @objcMembers public final class IterableJwtGenerator: NSObject { |
129 | 12 |
|
130 | 13 | /// Base64 URL encode without padding (URL-safe base64 encoding for JWT) |
|
0 commit comments