aboutsummaryrefslogtreecommitdiff
path: root/src/privet/auth_manager.h
blob: ce406cecc84871278c5c6fd3d508138f416f5ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_
#define LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_

#include <deque>
#include <string>
#include <vector>

#include <base/time/default_clock.h>
#include <base/time/time.h>
#include <weave/error.h>

#include "src/privet/privet_types.h"

namespace weave {

class Config;
enum class RootClientTokenOwner;

namespace privet {

class AuthManager {
 public:
  AuthManager(Config* config,
              const std::vector<uint8_t>& certificate_fingerprint);

  // Constructor for tests.
  AuthManager(const std::vector<uint8_t>& secret,
              const std::vector<uint8_t>& certificate_fingerprint,
              base::Clock* clock = nullptr);
  ~AuthManager();

  std::vector<uint8_t> CreateAccessToken(const UserInfo& user_info);
  UserInfo ParseAccessToken(const std::vector<uint8_t>& token,
                            base::Time* time) const;

  const std::vector<uint8_t>& GetSecret() const { return secret_; }
  const std::vector<uint8_t>& GetCertificateFingerprint() const {
    return certificate_fingerprint_;
  }

  base::Time Now() const;

  std::vector<uint8_t> ClaimRootClientAuthToken(RootClientTokenOwner owner,
                                                ErrorPtr* error);
  bool ConfirmAuthToken(const std::vector<uint8_t>& token, ErrorPtr* error);

  std::vector<uint8_t> GetRootClientAuthToken() const;
  bool IsValidAuthToken(const std::vector<uint8_t>& token) const;

  void SetSecret(const std::vector<uint8_t>& secret,
                 RootClientTokenOwner owner);

 private:
  Config* config_{nullptr};
  base::DefaultClock default_clock_;
  base::Clock* clock_{&default_clock_};

  std::vector<uint8_t> secret_;
  std::vector<uint8_t> certificate_fingerprint_;

  std::deque<std::pair<std::unique_ptr<AuthManager>, RootClientTokenOwner>>
      pending_claims_;

  DISALLOW_COPY_AND_ASSIGN(AuthManager);
};

}  // namespace privet
}  // namespace weave

#endif  // LIBWEAVE_SRC_PRIVET_AUTH_MANAGER_H_