Skip to content

Commit 97ca800

Browse files
committed
Added a Claim type to the implementation
1 parent 0d6f0e8 commit 97ca800

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
struct Claim: Identifiable {
3+
let key: String
4+
let value: String
5+
var id: String {key}
6+
}

Samples/Swift/DaysUntilBirthday/Shared/ViewModels/AuthenticationViewModel.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ final class AuthenticationViewModel: ObservableObject {
2828

2929
/// The user's `claims` as found in `idToken`.
3030
/// - note: If the user is logged out, then this will default to empty.
31-
var claims: [(key: String, value: String)] {
31+
var claims: [Claim] {
3232
switch state {
3333
case .signedIn(let user):
3434
guard let idToken = user.idToken?.tokenString else { return [] }
@@ -86,7 +86,7 @@ final class AuthenticationViewModel: ObservableObject {
8686

8787
private extension AuthenticationViewModel {
8888
/// Returns a collection of formatted claim keys and values decoded from a JWT.
89-
func decodeClaims(fromJwt jwt: String) -> [(key: String, value: String)] {
89+
func decodeClaims(fromJwt jwt: String) -> [Claim] {
9090
let segments = jwt.components(separatedBy: ".")
9191

9292
guard segments.count > 1,
@@ -95,7 +95,7 @@ private extension AuthenticationViewModel {
9595
return []
9696
}
9797

98-
let claims: [(key: String, value: String)?] = [
98+
let claims: [Claim?] = [
9999
formatAuthTime(from: payload),
100100
formatAmr(from: payload)
101101
]
@@ -128,23 +128,23 @@ private extension AuthenticationViewModel {
128128
}
129129

130130
/// Returns the `auth_time` claim from the given JWT, if present.
131-
func formatAuthTime(from payload: [String: Any]) -> (key: String, value: String)? {
131+
func formatAuthTime(from payload: [String: Any]) -> Claim? {
132132
guard let authTime = payload["auth_time"] as? TimeInterval
133133
else {
134134
return nil
135135
}
136136
let date = Date(timeIntervalSince1970: authTime)
137137
let formattedDate = DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .medium)
138-
return (key: "auth_time", value: formattedDate)
138+
return Claim(key: "auth_time", value: formattedDate)
139139
}
140140

141141
/// Returns the `amr` claim from the given JWT, if present.
142-
private func formatAmr(from payload: [String: Any]) -> (key: String, value: String)? {
142+
private func formatAmr(from payload: [String: Any]) -> Claim? {
143143
guard let amr = payload["amr"] as? [String]
144144
else {
145145
return nil
146146
}
147-
return (key: "amr", value: amr.joined(separator: ", "))
147+
return Claim(key: "amr", value: amr.joined(separator: ", "))
148148
}
149149
}
150150

Samples/Swift/DaysUntilBirthday/iOS/UserProfileView.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import GoogleSignIn
1919

2020
/// A view that displays a list of ID token claims.
2121
struct ClaimsListView: View {
22-
let claims: [(key: String, value: String)]
22+
let claims: [Claim]
2323
var body: some View {
24-
List(claims, id: \.key) { claim in
24+
List(claims) { claim in
2525
VStack(alignment: .leading, spacing: 4) {
2626
Text(claim.key)
2727
.font(.caption)
@@ -63,7 +63,6 @@ struct UserProfileView: View {
6363
Image(systemName: "list.bullet.rectangle.portrait")
6464
Text("View ID Token Claims")
6565
}
66-
.font(.caption)
6766
.foregroundColor(.blue)
6867
}
6968
}

0 commit comments

Comments
 (0)