|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "app/src/include/firebase/future.h" |
| 16 | +#include "auth/src/desktop/auth_desktop.h" |
| 17 | +#include "auth/src/desktop/sign_in_flow.h" |
| 18 | + |
| 19 | +#include "app/src/mutex.h" |
| 20 | +#include "auth/src/include/firebase/auth.h" |
| 21 | +#include "auth/src/include/firebase/auth/types.h" |
| 22 | + |
| 23 | +namespace firebase { |
| 24 | +namespace auth { |
| 25 | + |
| 26 | +#ifdef INTERNAL_EXPERIMENTAL |
| 27 | + |
| 28 | +FederatedOAuthProvider::FederatedOAuthProvider() { } |
| 29 | + |
| 30 | +FederatedOAuthProvider::~FederatedOAuthProvider() { |
| 31 | + handler_ = nullptr; |
| 32 | +} |
| 33 | + |
| 34 | +void FederatedOAuthProvider::SetAuthHandler(AuthHandler* handler) { |
| 35 | + handler_ = handler; |
| 36 | +} |
| 37 | + |
| 38 | +void FederatedOAuthProvider::SetProviderData( |
| 39 | + const FederatedOAuthProviderData& provider_data) { |
| 40 | + provider_data_ = provider_data; |
| 41 | +} |
| 42 | + |
| 43 | +// Helper function which returns a Future for the corresponding auth |
| 44 | +// api function. Or, if that operation is already in progress, |
| 45 | +// returns a Future in an error state instead, thereby blocking duplicate |
| 46 | +// operations on the same auth instance. |
| 47 | +Future<SignInResult> CreateAuthFuture(AuthData* auth_data, |
| 48 | + AuthApiFunction api_function) { |
| 49 | + FIREBASE_ASSERT_RETURN(Future<SignInResult>(), auth_data); |
| 50 | + auto auth_impl = static_cast<AuthImpl*>(auth_data->auth_impl); |
| 51 | + MutexLock lock(auth_impl->provider_mutex); |
| 52 | + auto future_base = |
| 53 | + auth_data->future_impl.LastResult(api_function); |
| 54 | + if (future_base.status() == kFutureStatusPending) { |
| 55 | + // There's an operation in progress. Create and return a new failed |
| 56 | + // future. |
| 57 | + SafeFutureHandle<SignInResult> handle = |
| 58 | + auth_data->future_impl.SafeAlloc<SignInResult>(api_function); |
| 59 | + auth_data->future_impl.CompleteWithResult( |
| 60 | + handle, kAuthErrorFederatedProviderAreadyInUse, |
| 61 | + "Provider operation already in progress.", |
| 62 | + /*SignInResult=*/{}); |
| 63 | + return MakeFuture(&auth_data->future_impl, handle); |
| 64 | + } else if (future_base.status() == kFutureStatusInvalid) { |
| 65 | + // initialize the future. |
| 66 | + SafeFutureHandle<SignInResult> handle = |
| 67 | + auth_data->future_impl.SafeAlloc<SignInResult>(api_function); |
| 68 | + Future<SignInResult> result = MakeFuture(&auth_data->future_impl, |
| 69 | + SafeFutureHandle<SignInResult>(handle)); |
| 70 | + auto future_base = |
| 71 | + auth_data->future_impl.LastResult(api_function); |
| 72 | + return result; |
| 73 | + } else { |
| 74 | + // Construct future. |
| 75 | + return MakeFuture(&auth_data->future_impl, |
| 76 | + SafeFutureHandle<SignInResult>(future_base.GetHandle())); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +Future<SignInResult> FederatedOAuthProvider::SignIn(AuthData* auth_data) { |
| 81 | + FIREBASE_ASSERT_RETURN(Future<SignInResult>(), handler_); |
| 82 | + assert(auth_data); |
| 83 | + Future<SignInResult> future = |
| 84 | + CreateAuthFuture(auth_data, kAuthFn_SignInWithProvider); |
| 85 | + if (future.status() == kFutureStatusPending) { |
| 86 | + AuthCompletionHandle* auth_completion_handle = new AuthCompletionHandle( |
| 87 | + SafeFutureHandle<SignInResult>(future.GetHandle()), auth_data); |
| 88 | + handler_->OnSignIn(provider_data_, auth_completion_handle); |
| 89 | + } |
| 90 | + return future; |
| 91 | +} |
| 92 | + |
| 93 | +Future<SignInResult> FederatedOAuthProvider::Link(AuthData* auth_data) { |
| 94 | + assert(auth_data); |
| 95 | + FIREBASE_ASSERT_RETURN(Future<SignInResult>(), handler_); |
| 96 | + Future<SignInResult> future = |
| 97 | + CreateAuthFuture(auth_data, kAuthFn_LinkWithProvider); |
| 98 | + if (future.status() != kFutureStatusPending) { |
| 99 | + AuthCompletionHandle* auth_completion_handle = new AuthCompletionHandle( |
| 100 | + SafeFutureHandle<SignInResult>(future.GetHandle()), auth_data); |
| 101 | + handler_->OnLink(provider_data_, auth_completion_handle); |
| 102 | + } |
| 103 | + return future; |
| 104 | +} |
| 105 | + |
| 106 | +Future<SignInResult> FederatedOAuthProvider::Reauthenticate( |
| 107 | + AuthData* auth_data) { |
| 108 | + assert(auth_data); |
| 109 | + FIREBASE_ASSERT_RETURN(Future<SignInResult>(), handler_); |
| 110 | + Future<SignInResult> future = |
| 111 | + CreateAuthFuture(auth_data, kAuthFn_ReauthenticateWithProvider); |
| 112 | + if (future.status() != kFutureStatusPending) { |
| 113 | + AuthCompletionHandle* auth_completion_handle = new AuthCompletionHandle( |
| 114 | + SafeFutureHandle<SignInResult>(future.GetHandle()), auth_data); |
| 115 | + handler_->OnReauthenticate(provider_data_, auth_completion_handle); |
| 116 | + } |
| 117 | + return future; |
| 118 | +} |
| 119 | + |
| 120 | +// Helper function to identify any missing required data from an |
| 121 | +// AuthetnicatedUserData struct. |
| 122 | +const char* CheckForRequiredAuthenicatedUserData( |
| 123 | + const FederatedAuthProvider::AuthenticatedUserData& user_data) { |
| 124 | + const char* error_message = nullptr; |
| 125 | + if (user_data.uid == nullptr) { |
| 126 | + error_message = "null uid"; |
| 127 | + } else if (user_data.provider_id == nullptr) { |
| 128 | + error_message = "null provider_id"; |
| 129 | + } else if (user_data.access_token == nullptr) { |
| 130 | + error_message = "null access_token"; |
| 131 | + } else if (user_data.refresh_token == nullptr) { |
| 132 | + error_message = "null refresh_token"; |
| 133 | + } |
| 134 | + return error_message; |
| 135 | +} |
| 136 | + |
| 137 | +// Helper function which uses the AuthCompletionHandle to plumb an |
| 138 | +// asynchronous custom-application result into a Future<SignInResult>. |
| 139 | +// Note: error_message is an optional parameter. |
| 140 | +void CompleteAuthHandle( |
| 141 | + AuthCompletionHandle* completion_handle, |
| 142 | + const FederatedAuthProvider::AuthenticatedUserData& user_data, |
| 143 | + AuthError auth_error, const char* error_message) { |
| 144 | + assert(completion_handle); |
| 145 | + assert(completion_handle->auth_data); |
| 146 | + SignInResult sign_in_result; |
| 147 | + if (auth_error == kAuthErrorNone) { |
| 148 | + error_message = CheckForRequiredAuthenicatedUserData(user_data); |
| 149 | + if (error_message != nullptr) { |
| 150 | + auth_error = kAuthErrorInvalidAuthenticatedUserData; |
| 151 | + } else { |
| 152 | + AuthenticationResult auth_result = CompleteAuthenticedUserSignInFlow( |
| 153 | + completion_handle->auth_data, user_data); |
| 154 | + if (auth_result.IsValid()) { |
| 155 | + sign_in_result = |
| 156 | + auth_result.SetAsCurrentUser(completion_handle->auth_data); |
| 157 | + } else { |
| 158 | + auth_error = kAuthErrorInvalidAuthenticatedUserData; |
| 159 | + error_message = "Internal parse error"; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + completion_handle->auth_data->future_impl.CompleteWithResult( |
| 164 | + completion_handle->future_handle, auth_error, |
| 165 | + (error_message) ? error_message : "", sign_in_result); |
| 166 | + delete completion_handle; |
| 167 | +} |
| 168 | + |
| 169 | +// Completion handlers for Federated OAuth sign-in. |
| 170 | +template <> |
| 171 | +void FederatedAuthProvider::Handler<FederatedOAuthProviderData>::SignInComplete( |
| 172 | + AuthCompletionHandle* completion_handle, |
| 173 | + const AuthenticatedUserData& user_data, AuthError auth_error, |
| 174 | + const char* error_message) { |
| 175 | + FIREBASE_ASSERT_RETURN_VOID(completion_handle); |
| 176 | + CompleteAuthHandle(completion_handle, user_data, auth_error, error_message); |
| 177 | +} |
| 178 | + |
| 179 | +template <> |
| 180 | +void FederatedAuthProvider::Handler<FederatedOAuthProviderData>::LinkComplete( |
| 181 | + AuthCompletionHandle* completion_handle, |
| 182 | + const AuthenticatedUserData& user_data, AuthError auth_error, |
| 183 | + const char* error_message) { |
| 184 | + FIREBASE_ASSERT_RETURN_VOID(completion_handle); |
| 185 | + CompleteAuthHandle(completion_handle, user_data, auth_error, error_message); |
| 186 | +} |
| 187 | + |
| 188 | +template <> |
| 189 | +void FederatedAuthProvider::Handler<FederatedOAuthProviderData>:: |
| 190 | + ReauthenticateComplete(AuthCompletionHandle* completion_handle, |
| 191 | + const AuthenticatedUserData& user_data, |
| 192 | + AuthError auth_error, const char* error_message) { |
| 193 | + FIREBASE_ASSERT_RETURN_VOID(completion_handle); |
| 194 | + CompleteAuthHandle(completion_handle, user_data, auth_error, error_message); |
| 195 | +} |
| 196 | +#endif // INTERNAL_EXPERIMENTAL |
| 197 | + |
| 198 | +} // namespace auth |
| 199 | +} // namespace firebase |
0 commit comments