@@ -163,9 +163,11 @@ public struct CredentialsManager {
163163 /// ```
164164 ///
165165 /// - Parameter audience: Identifier of the API the stored API credentials are for.
166+ /// - Parameter scope: Scope of the stored API credentials. If API credentials are fetched with scope, it is recommended to pass the scope to clear them.
166167 /// - Returns: If the API credentials were removed.
167- public func clear( forAudience audience: String ) -> Bool {
168- return self . storage. deleteEntry ( forKey: audience)
168+ public func clear( forAudience audience: String , scope: String ? = nil ) -> Bool {
169+ let key = getAPICredentialsStorageKey ( audience: audience, scope: scope)
170+ return self . storage. deleteEntry ( forKey: key)
169171 }
170172
171173 #if WEB_AUTH_PLATFORM
@@ -694,23 +696,34 @@ public struct CredentialsManager {
694696 callback: callback)
695697 }
696698
697- public func store( apiCredentials: APICredentials , forAudience audience: String ) -> Bool {
699+ public func store( apiCredentials: APICredentials , forAudience audience: String , forScope scope : String ? = nil ) -> Bool {
698700 guard let data = try ? apiCredentials. encode ( ) else {
699701 return false
700702 }
701703
702- return self . storage. setEntry ( data, forKey: audience)
704+ let key = getAPICredentialsStorageKey ( audience: audience, scope: scope)
705+ return self . storage. setEntry ( data, forKey: key)
703706 }
704707
705708 private func retrieveCredentials( ) -> Credentials ? {
706709 guard let data = self . storage. getEntry ( forKey: self . storeKey) else { return nil }
707710 return try ? NSKeyedUnarchiver . unarchivedObject ( ofClass: Credentials . self, from: data)
708711 }
709712
710- private func retrieveAPICredentials( audience: String ) -> APICredentials ? {
711- guard let data = self . storage. getEntry ( forKey: audience) else { return nil }
713+ private func retrieveAPICredentials( audience: String , scope: String ? ) -> APICredentials ? {
714+ let key = getAPICredentialsStorageKey ( audience: audience, scope: scope)
715+ guard let data = self . storage. getEntry ( forKey: key) else { return nil }
712716 return try ? APICredentials ( from: data)
713717 }
718+
719+ private func getAPICredentialsStorageKey( audience: String , scope: String ? ) -> String {
720+ // Use audience if scope is null else use a combination of audience and scope
721+ if let scope = scope {
722+ return " \( audience) :: \( scope. replacingOccurrences ( of: " " , with: " :: " ) ) "
723+ } else {
724+ return audience
725+ }
726+ }
714727
715728 // swiftlint:disable:next function_parameter_count
716729 private func retrieveCredentials( scope: String ? ,
@@ -832,10 +845,10 @@ public struct CredentialsManager {
832845 dispatchGroup. enter ( )
833846
834847 DispatchQueue . global ( qos: . userInitiated) . async {
835- if let apiCredentials = self . retrieveAPICredentials ( audience: audience) ,
848+ if let apiCredentials = self . retrieveAPICredentials ( audience: audience, scope : scope ) ,
836849 !self . hasExpired ( apiCredentials. expiresIn) ,
837850 !self . willExpire ( apiCredentials. expiresIn, within: minTTL) ,
838- !self . hasScopeChanged ( from: apiCredentials. scope, to: scope) {
851+ !self . hasScopeChanged ( from: apiCredentials. scope, to: scope, ignoreOpenid : scope ? . contains ( " openid " ) == false ) {
839852 dispatchGroup. leave ( )
840853 return callback ( . success( apiCredentials) )
841854 }
@@ -867,7 +880,7 @@ public struct CredentialsManager {
867880 } else if !self . store ( credentials: newCredentials) {
868881 dispatchGroup. leave ( )
869882 callback ( . failure( CredentialsManagerError ( code: . storeFailed) ) )
870- } else if !self . store ( apiCredentials: newAPICredentials, forAudience: audience) {
883+ } else if !self . store ( apiCredentials: newAPICredentials, forAudience: audience, forScope : scope ) {
871884 dispatchGroup. leave ( )
872885 callback ( . failure( CredentialsManagerError ( code: . storeFailed) ) )
873886 } else {
@@ -893,14 +906,30 @@ public struct CredentialsManager {
893906 return expiresIn < Date ( )
894907 }
895908
896- func hasScopeChanged( from lastScope: String ? , to newScope: String ? ) -> Bool {
909+ func hasScopeChanged( from lastScope: String ? , to newScope: String ? , ignoreOpenid: Bool = false ) -> Bool {
910+
897911 if let lastScope = lastScope, let newScope = newScope {
898- let lastScopeList = lastScope. lowercased ( ) . split ( separator: " " ) . sorted ( )
899- let newScopeList = newScope. lowercased ( ) . split ( separator: " " ) . sorted ( )
900-
901- return lastScopeList != newScopeList
912+
913+ var storedScopes = Set (
914+ lastScope
915+ . split ( separator: " " )
916+ . filter { !$0. isEmpty }
917+ . map { String ( $0) . lowercased ( ) }
918+ )
919+
920+ if ignoreOpenid {
921+ storedScopes. remove ( " openid " )
922+ }
923+
924+ let requiredScopes = Set (
925+ newScope
926+ . split ( separator: " " )
927+ . filter { !$0. isEmpty }
928+ . map { String ( $0) . lowercased ( ) }
929+ )
930+
931+ return storedScopes != requiredScopes
902932 }
903-
904933 return false
905934 }
906935
0 commit comments