Skip to content

Commit

Permalink
Save auth tokens to prefs.
Browse files Browse the repository at this point in the history
  • Loading branch information
kannanvijayan committed Dec 20, 2023
1 parent 8c23208 commit fb8694b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
// found in the LICENSE file.

#include "components/record_replay/services/auth_token/public/cpp/auth_token_service.h"

#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "content/public/browser/service_process_host.h"

namespace {
const char kRecordReplayPrefsKey[] = "record_replay";
}

namespace auth_token {

RecordReplayAuthTokenService::RecordReplayAuthTokenService() = default;
RecordReplayAuthTokenService::RecordReplayAuthTokenService(Profile* profile)
: profile_(profile) {}
RecordReplayAuthTokenService::~RecordReplayAuthTokenService() = default;

void RecordReplayAuthTokenService::BindAuthTokenStore(
Expand All @@ -16,19 +25,37 @@ void RecordReplayAuthTokenService::BindAuthTokenStore(
auth_token_stores_.Add(this, std::move(store));
}

void RecordReplayAuthTokenService::SetToken(const std::string& token) {
token_ = token;
NotifyObservers();
void RecordReplayAuthTokenService::SetUserToken(const std::string& user_token) {
ScopedDictPrefUpdate record_replay_prefs(profile_->GetPrefs(),
kRecordReplayPrefsKey);
record_replay_prefs->SetByDottedPath("user_token", user_token);
user_token_ = user_token;
NotifyObserversAboutUserToken();
}

void RecordReplayAuthTokenService::SetRefreshToken(const std::string& refresh_token) {
ScopedDictPrefUpdate record_replay_prefs(profile_->GetPrefs(),
kRecordReplayPrefsKey);
record_replay_prefs->SetByDottedPath("refresh_token", refresh_token);
refresh_token_ = refresh_token;
NotifyObserversAboutRefreshToken();
}

void RecordReplayAuthTokenService::AddObserver(mojo::PendingRemote<mojom::RecordReplayAuthTokenStoreObserver> observer) {
observers_.Add(std::move(observer));
NotifyObservers();
NotifyObserversAboutUserToken();
NotifyObserversAboutRefreshToken();
}

void RecordReplayAuthTokenService::NotifyObserversAboutUserToken() {
for (auto& observer : observers_) {
observer->OnRecordReplayAuthTokenChanged(user_token_);
}
}

void RecordReplayAuthTokenService::NotifyObservers() {
void RecordReplayAuthTokenService::NotifyObserversAboutRefreshToken() {
for (auto& observer : observers_) {
observer->OnRecordReplayAuthTokenChanged(token_);
observer->OnRecordReplayRefreshTokenChanged(refresh_token_);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote_set.h"

class Profile;

namespace auth_token {

class RecordReplayAuthTokenService : public KeyedService, public mojom::RecordReplayAuthTokenStore {
public:
RecordReplayAuthTokenService();
RecordReplayAuthTokenService(Profile* profile);
RecordReplayAuthTokenService(const RecordReplayAuthTokenService&) = delete;
RecordReplayAuthTokenService& operator=(const RecordReplayAuthTokenService&) = delete;
~RecordReplayAuthTokenService() override;
Expand All @@ -26,15 +28,19 @@ class RecordReplayAuthTokenService : public KeyedService, public mojom::RecordRe
mojo::PendingReceiver<mojom::RecordReplayAuthTokenStore> store);

// mojom::RecordReplayAuthTokenStore:
void SetToken(const std::string& token) override;
void SetUserToken(const std::string& user_token) override;
void SetRefreshToken(const std::string& refresh_token) override;
void AddObserver(mojo::PendingRemote<mojom::RecordReplayAuthTokenStoreObserver> observer) override;

private:
raw_ptr<Profile> profile_;
mojo::ReceiverSet<mojom::RecordReplayAuthTokenStore> auth_token_stores_;

std::string token_;
std::string user_token_;
std::string refresh_token_;

void NotifyObservers();
void NotifyObserversAboutUserToken();
void NotifyObserversAboutRefreshToken();

mojo::RemoteSet<mojom::RecordReplayAuthTokenStoreObserver> observers_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "components/record_replay/services/auth_token/public/cpp/auth_token_service_factory.h"

#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"

Expand Down Expand Up @@ -31,8 +32,9 @@ RecordReplayAuthTokenServiceFactory::RecordReplayAuthTokenServiceFactory()
RecordReplayAuthTokenServiceFactory::~RecordReplayAuthTokenServiceFactory() = default;

KeyedService* RecordReplayAuthTokenServiceFactory::BuildServiceInstanceFor(
content::BrowserContext* /*context*/) const {
return new auth_token::RecordReplayAuthTokenService();
content::BrowserContext* context) const {
return new auth_token::RecordReplayAuthTokenService(
Profile::FromBrowserContext(context));
}

// Incognito profiles should use their own instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ module auth_token.mojom;

// Interface to passing updated token around
interface RecordReplayAuthTokenStore {
SetToken(string token);
SetUserToken(string user_token);
SetRefreshToken(string refresh_token);

AddObserver(pending_remote<RecordReplayAuthTokenStoreObserver> observer);
// how do we remove?
};

interface RecordReplayAuthTokenStoreObserver {
OnRecordReplayAuthTokenChanged(string token);
OnRecordReplayRefreshTokenChanged(string refresh_token);
};
47 changes: 47 additions & 0 deletions third_party/blink/renderer/core/frame/local_frame_mojo_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,51 @@ void LocalFrameMojoHandler::OnRecordReplayAuthTokenChanged(const WTF::String& to
);
}

void LocalFrameMojoHandler::OnRecordReplayRefreshTokenChanged(const WTF::String& token) {
v8::Isolate* isolate = ToIsolate(frame_);
v8::HandleScope handle_scope(isolate);

ScriptState* script_state = ToScriptStateForMainWorld(frame_);
v8::Local<v8::Context> context = script_state->GetContext();
v8::Context::Scope context_scope(context);

// build up a JS object corresponding to the structure the devtools
// expects to receive:
//
// detail = {
// message: {
// token: "...",
// error?: "...",
// }
// }
//
// we don't currently receive an error from the auth token service, so we don't fill it in.
// if there was an error, presumably we wouldn't be called here.

v8::Local<v8::Object> message = v8::Object::New(isolate);
message->Set(context, V8String(isolate, "token"),
V8String(isolate, token))
.Check();

v8::Local<v8::Object> detail = v8::Object::New(isolate);
detail->Set(context, V8String(isolate, "message"), message)
.Check();


ExceptionState exception_state(isolate, ExceptionState::kExecutionContext,
"LocalFrameMojoHandler", "OnTokenChanged");

CustomEventInit* ev_init = CustomEventInit::Create(isolate, v8::Null(isolate), exception_state);
// bail if the creator of the event threw an exception
if (exception_state.HadException()) {
return;
}

ev_init->setDetail(ScriptValue::From(script_state, detail));

frame_->DomWindow()->DispatchEvent(
*CustomEvent::Create(script_state, "WebChannelMessageToContent", ev_init)
);
}

} // namespace blink
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class LocalFrameMojoHandler

// RecordReplayAuthTokenStoreObserver implementation:
void OnRecordReplayAuthTokenChanged(const WTF::String& token) final;
void OnRecordReplayRefreshTokenChanged(const WTF::String& token) final;

Member<blink::LocalFrame> frame_;

Expand Down

0 comments on commit fb8694b

Please sign in to comment.