• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_H_
6 #define NET_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_H_
7 
8 #include <memory>
9 
10 #include "base/functional/callback_forward.h"
11 #include "net/base/net_export.h"
12 #include "net/device_bound_sessions/registration_fetcher_param.h"
13 #include "net/device_bound_sessions/session.h"
14 #include "net/device_bound_sessions/session_challenge_param.h"
15 #include "net/device_bound_sessions/session_key.h"
16 
17 namespace net {
18 class IsolationInfo;
19 class URLRequest;
20 class URLRequestContext;
21 }
22 
23 namespace net::device_bound_sessions {
24 
25 // Main class for Device Bound Session Credentials (DBSC).
26 // Full information can be found at https://github.com/WICG/dbsc
27 class NET_EXPORT SessionService {
28  public:
29   using RefreshCompleteCallback = base::OnceClosure;
30   using OnAccessCallback = base::RepeatingCallback<void(const SessionKey&)>;
31 
32   // Returns nullptr if unexportable key provider is not supported by the
33   // platform or the device.
34   static std::unique_ptr<SessionService> Create(
35       const URLRequestContext* request_context);
36 
37   SessionService(const SessionService&) = delete;
38   SessionService& operator=(const SessionService&) = delete;
39 
40   virtual ~SessionService() = default;
41 
42   // Called to register a new session after getting a Sec-Session-Registration
43   // header.
44   // Registration parameters to be used for creating the registration
45   // request.
46   // Isolation info to be used for registration request, this should be the
47   // same as was used for the response with the Sec-Session-Registration
48   // header.
49   virtual void RegisterBoundSession(
50       OnAccessCallback on_access_callback,
51       RegistrationFetcherParam registration_params,
52       const IsolationInfo& isolation_info) = 0;
53 
54   // Check if a request should be deferred due to the session cookie being
55   // missing. This should only be called once the request has the correct
56   // cookies added to the request.
57   // If multiple sessions needs to be refreshed for this request,
58   // any of them can be returned.
59   // Returns the session id if the request should be deferred, returns
60   // std::nullopt if the request does not need to be deferred.
61   virtual std::optional<Session::Id> GetAnySessionRequiringDeferral(
62       URLRequest* request) = 0;
63 
64   // Defer a request while refreshing the session.
65   // session_id is the identifier of the session that is required to be
66   // refreshed.
67   // Provides two callbacks, will always call one of them:
68   // - The first will query for cookies for the requests again.
69   // - The second to send the request without query for cookies again.
70   virtual void DeferRequestForRefresh(
71       URLRequest* request,
72       Session::Id session_id,
73       RefreshCompleteCallback restart_callback,
74       RefreshCompleteCallback continue_callback) = 0;
75 
76   // Set the challenge for a bound session after getting a
77   // Sec-Session-Challenge header.
78   virtual void SetChallengeForBoundSession(
79       OnAccessCallback on_access_callback,
80       const GURL& request_url,
81       const SessionChallengeParam& param) = 0;
82 
83   // Get all sessions. If sessions have not yet been loaded from disk,
84   // defer until completely initialized.
85   virtual void GetAllSessionsAsync(
86       base::OnceCallback<void(const std::vector<SessionKey>&)> callback) = 0;
87 
88   // Delete the session matching `key`.
89   virtual void DeleteSession(const SchemefulSite& site,
90                              const Session::Id& id) = 0;
91 
92  protected:
93   SessionService() = default;
94 };
95 
96 }  // namespace net::device_bound_sessions
97 
98 #endif  // NET_DEVICE_BOUND_SESSIONS_SESSION_SERVICE_H_
99