GoogleSignInAuthenticator.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import GoogleSignIn
  18. /// An observable class for authenticating via Google.
  19. final class GoogleSignInAuthenticator: ObservableObject {
  20. private var authViewModel: AuthenticationViewModel
  21. /// Creates an instance of this authenticator.
  22. /// - parameter authViewModel: The view model this authenticator will set logged in status on.
  23. init(authViewModel: AuthenticationViewModel) {
  24. self.authViewModel = authViewModel
  25. }
  26. /// Signs in the user based upon the selected account.'
  27. /// - note: Successful calls to this will set the `authViewModel`'s `state` property.
  28. @MainActor func signIn() {
  29. #if os(iOS)
  30. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  31. print("There is no root view controller!")
  32. return
  33. }
  34. let manualNonce = UUID().uuidString
  35. GIDSignIn.sharedInstance.signIn(
  36. withPresenting: rootViewController,
  37. hint: nil,
  38. additionalScopes: nil,
  39. nonce: manualNonce
  40. ) { signInResult, error in
  41. guard let signInResult = signInResult else {
  42. print("Error! \(String(describing: error))")
  43. return
  44. }
  45. // Per OpenID Connect Core section 3.1.3.7, rule #11, compare returned nonce to manual
  46. guard let idToken = signInResult.user.idToken?.tokenString,
  47. let returnedNonce = self.decodeNonce(fromJWT: idToken),
  48. returnedNonce == manualNonce else {
  49. // Assert a failure for convenience so that integration tests with this sample app fail upon
  50. // `nonce` mismatch
  51. assertionFailure("ERROR: Returned nonce doesn't match manual nonce!")
  52. return
  53. }
  54. self.authViewModel.state = .signedIn(signInResult.user)
  55. }
  56. #elseif os(macOS)
  57. guard let presentingWindow = NSApplication.shared.windows.first else {
  58. print("There is no presenting window!")
  59. return
  60. }
  61. GIDSignIn.sharedInstance.signIn(withPresenting: presentingWindow) { signInResult, error in
  62. guard let signInResult = signInResult else {
  63. print("Error! \(String(describing: error))")
  64. return
  65. }
  66. self.authViewModel.state = .signedIn(signInResult.user)
  67. }
  68. #endif
  69. }
  70. /// Signs out the current user.
  71. func signOut() {
  72. GIDSignIn.sharedInstance.signOut()
  73. authViewModel.state = .signedOut
  74. }
  75. /// Disconnects the previously granted scope and signs the user out.
  76. func disconnect() {
  77. GIDSignIn.sharedInstance.disconnect { error in
  78. if let error = error {
  79. print("Encountered error disconnecting scope: \(error).")
  80. }
  81. self.signOut()
  82. }
  83. }
  84. // Confines birthday calucation to iOS for now.
  85. /// Adds the birthday read scope for the current user.
  86. /// - parameter completion: An escaping closure that is called upon successful completion of the
  87. /// `addScopes(_:presenting:)` request.
  88. /// - note: Successful requests will update the `authViewModel.state` with a new current user that
  89. /// has the granted scope.
  90. @MainActor func addBirthdayReadScope(completion: @escaping () -> Void) {
  91. guard let currentUser = GIDSignIn.sharedInstance.currentUser else {
  92. fatalError("No user signed in!")
  93. }
  94. #if os(iOS)
  95. guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
  96. fatalError("No root view controller!")
  97. }
  98. currentUser.addScopes([BirthdayLoader.birthdayReadScope],
  99. presenting: rootViewController) { signInResult, error in
  100. if let error = error {
  101. print("Found error while adding birthday read scope: \(error).")
  102. return
  103. }
  104. guard let signInResult = signInResult else { return }
  105. self.authViewModel.state = .signedIn(signInResult.user)
  106. completion()
  107. }
  108. #elseif os(macOS)
  109. guard let presentingWindow = NSApplication.shared.windows.first else {
  110. fatalError("No presenting window!")
  111. }
  112. currentUser.addScopes([BirthdayLoader.birthdayReadScope],
  113. presenting: presentingWindow) { signInResult, error in
  114. if let error = error {
  115. print("Found error while adding birthday read scope: \(error).")
  116. return
  117. }
  118. guard let signInResult = signInResult else { return }
  119. self.authViewModel.state = .signedIn(signInResult.user)
  120. completion()
  121. }
  122. #endif
  123. }
  124. }
  125. // MARK: Parse nonce from JWT ID Token
  126. private extension GoogleSignInAuthenticator {
  127. func decodeNonce(fromJWT jwt: String) -> String? {
  128. let segments = jwt.components(separatedBy: ".")
  129. guard let parts = decodeJWTSegment(segments[1]),
  130. let nonce = parts["nonce"] as? String else {
  131. return nil
  132. }
  133. return nonce
  134. }
  135. func decodeJWTSegment(_ segment: String) -> [String: Any]? {
  136. guard let segmentData = base64UrlDecode(segment),
  137. let segmentJSON = try? JSONSerialization.jsonObject(with: segmentData, options: []),
  138. let payload = segmentJSON as? [String: Any] else {
  139. return nil
  140. }
  141. return payload
  142. }
  143. func base64UrlDecode(_ value: String) -> Data? {
  144. var base64 = value
  145. .replacingOccurrences(of: "-", with: "+")
  146. .replacingOccurrences(of: "_", with: "/")
  147. let length = Double(base64.lengthOfBytes(using: String.Encoding.utf8))
  148. let requiredLength = 4 * ceil(length / 4.0)
  149. let paddingLength = requiredLength - length
  150. if paddingLength > 0 {
  151. let padding = "".padding(toLength: Int(paddingLength), withPad: "=", startingAt: 0)
  152. base64 = base64 + padding
  153. }
  154. return Data(base64Encoded: base64, options: .ignoreUnknownCharacters)
  155. }
  156. }