@@ -85,42 +85,19 @@ final class AuthenticationViewModel: ObservableObject {
8585}
8686
8787private extension AuthenticationViewModel {
88- /// Returns the `auth_time` claim from the given JWT, if present .
89- func decodeAuthTime ( fromJwt jwt: String ) -> Date ? {
88+ /// Returns a collection of formatted claim keys and values decoded from a JWT .
89+ func decodeClaims ( fromJwt jwt: String ) -> [ ( key : String , value : String ) ] {
9090 let segments = jwt. components ( separatedBy: " . " )
91- guard let parts = decodeJWTSegment ( segments [ 1 ] ) ,
92- let authTime = parts [ " auth_time " ] as? TimeInterval
93- else {
94- return nil
95- }
96- return Date ( timeIntervalSince1970: authTime)
97- }
9891
99- /// Returns the `amr` claim from the given JWT, if present.
100- func decodeAmr( fromJwt jwt: String ) -> [ String ] ? {
101- let segments = jwt. components ( separatedBy: " . " )
102- guard let parts = decodeJWTSegment ( segments [ 1 ] ) ,
103- let amr = parts [ " amr " ] as? [ String ]
92+ guard segments. count > 1 ,
93+ let payload = decodeJWTSegment ( segments [ 1 ] )
10494 else {
105- return nil
95+ return [ ]
10696 }
107- return amr
108- }
10997
110- /// Returns a collection of formatted claim keys and values decoded from a JWT.
111- func decodeClaims( fromJwt jwt: String ) -> [ ( key: String , value: String ) ] {
11298 let claims : [ ( key: String , value: String ) ? ] = [
113- decodeAuthTime ( fromJwt: jwt) . map { date in
114- let formattedDate = DateFormatter . localizedString (
115- from: date,
116- dateStyle: . medium,
117- timeStyle: . medium
118- )
119- return ( key: " auth_time " , value: formattedDate)
120- } ,
121- decodeAmr ( fromJwt: jwt) . map { amr in
122- ( key: " amr " , value: amr. joined ( separator: " , " ) )
123- } ,
99+ formatAuthTime ( from: payload) ,
100+ formatAmr ( from: payload)
124101 ]
125102
126103 return claims. compactMap { $0 }
@@ -149,6 +126,26 @@ private extension AuthenticationViewModel {
149126 }
150127 return Data ( base64Encoded: base64, options: . ignoreUnknownCharacters)
151128 }
129+
130+ /// Returns the `auth_time` claim from the given JWT, if present.
131+ func formatAuthTime( from payload: [ String : Any ] ) -> ( key: String , value: String ) ? {
132+ guard let authTime = payload [ " auth_time " ] as? TimeInterval
133+ else {
134+ return nil
135+ }
136+ let date = Date ( timeIntervalSince1970: authTime)
137+ let formattedDate = DateFormatter . localizedString ( from: date, dateStyle: . medium, timeStyle: . medium)
138+ return ( key: " auth_time " , value: formattedDate)
139+ }
140+
141+ /// Returns the `amr` claim from the given JWT, if present.
142+ private func formatAmr( from payload: [ String : Any ] ) -> ( key: String , value: String ) ? {
143+ guard let amr = payload [ " amr " ] as? [ String ]
144+ else {
145+ return nil
146+ }
147+ return ( key: " amr " , value: amr. joined ( separator: " , " ) )
148+ }
152149}
153150
154151extension AuthenticationViewModel {
0 commit comments