Size: 4802 bytes.


  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
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// cs/apps/trycopilot.ai/api/auth.cc
#include "cs/apps/trycopilot.ai/api/auth.hh"

#include "cs/apps/trycopilot.ai/protos/auth.proto.hh"
#include "cs/apps/trycopilot.ai/protos/gencode/auth.proto.hh"
#include "cs/apps/trycopilot.ai/protos/gencode/auth.validate.hh"
#include "cs/apps/trycopilot.ai/protos/gencode/user.proto.hh"
#include "cs/apps/trycopilot.ai/protos/user.proto.hh"
#include "cs/log.hh"
#include "cs/net/http/request.hh"
#include "cs/net/http/response.hh"
#include "cs/net/http/web_app.hh"
#include "cs/net/proto/db/client.gpt.hh"
#include "cs/net/proto/db/query_helpers.gpt.hh"
#include "cs/net/proto/db/query_view.gpt.hh"
#include "cs/net/rpc/rpc.hh"
#include "cs/parsers/parsers.hh"
#include "cs/result.hh"
#include "cs/util/context.hh"
#include "cs/util/di/context.gpt.hh"
#include "cs/util/time.hh"
#include "cs/util/uuid.hh"

namespace {  // use_usings
using ::cs::InvalidArgument;
using ::cs::Result;
using ::cs::ResultOr;
using ::cs::ValidationError;
using ::cs::apps::trycopilotai::protos::LoginRequest;
using ::cs::apps::trycopilotai::protos::LoginResponse;
using ::cs::apps::trycopilotai::protos::LogoutRequest;
using ::cs::apps::trycopilotai::protos::LogoutResponse;
using ::cs::apps::trycopilotai::protos::Token;
using ::cs::apps::trycopilotai::protos::User;
using ::cs::net::http::HtmlResponse;
using ::cs::net::http::HTTP_200_OK;
using ::cs::net::http::Request;
using ::cs::net::http::Response;
using ::cs::net::proto::db::EQUALS;
using ::cs::net::proto::db::IDatabaseClient;
using ::cs::net::proto::db::QueryView;
using ::cs::net::proto::db::Upsert;
using ::cs::net::proto::validation::Validate;
using ::cs::util::di::Context;
}  // namespace

namespace {  // impl
// djb2
std::string HashPassword(std::string password,
                         std::string salt) {
  unsigned long hash = 5381;
  auto str = salt + password + salt;
  for (char c : str) {
    hash = ((hash << 5) + hash) + c;  // hash * 33 + c
  }

  std::stringstream ss;
  ss << std::hex << hash;
  return ss.str();
}
}  // namespace

namespace cs::apps::trycopilotai::api {

using ::cs::InvalidArgument;
using ::cs::Result;
using ::cs::ResultOr;
using ::cs::ValidationError;
using ::cs::apps::trycopilotai::protos::LoginRequest;
using ::cs::apps::trycopilotai::protos::LoginResponse;
using ::cs::apps::trycopilotai::protos::LogoutRequest;
using ::cs::apps::trycopilotai::protos::LogoutResponse;
using ::cs::apps::trycopilotai::protos::Token;
using ::cs::apps::trycopilotai::protos::User;
using ::cs::net::http::HtmlResponse;
using ::cs::net::http::HTTP_200_OK;
using ::cs::net::http::Request;
using ::cs::net::http::Response;
using ::cs::net::proto::db::EQUALS;
using ::cs::net::proto::db::IDatabaseClient;
using ::cs::net::proto::db::QueryView;
using ::cs::net::proto::db::Upsert;
using ::cs::net::proto::validation::Validate;
using ::cs::util::random::uuid::generate_uuid_v4;
using LoginRequestRules = ::cs::apps::trycopilotai::protos::
    gencode::auth::validation_generated::LoginRequestRules;

IMPLEMENT_RPC(LoginRPC, LoginRequest, LoginResponse) {
  auto validation = Validate(request, LoginRequestRules{});
  if (!validation.ok()) {
    return TRACE(validation);
  }
  SET_OR_RET(auto* ctx, GetContext());
  auto db = ctx->Get<IDatabaseClient>();
  SET_OR_RET(User user,
             QueryView<User>("users", db)
                 .where(EQUALS(&User::email, request.email))
                 .first());

  if (HashPassword(request.password, user.password.salt) !=
      user.password.hash) {
    return TRACE(InvalidArgument("Invalid password."));
  }

  QueryView<Token> tokens_query("tokens", db);
  ResultOr<Token> existing_token =
      tokens_query
          .where(EQUALS(&Token::user_uuid, user.uuid))
          .where(EQUALS(&Token::active, true))
          .first();
  Token token;
  if (Result::IsNotFound(existing_token)) {
    token.user_uuid = user.uuid;
    token.uuid = generate_uuid_v4();
    token.active = true;
    OK_OR_RET(Upsert("tokens", token, *db));
  } else if (existing_token.ok()) {
    token = existing_token.value();
  } else {
    return TRACE(existing_token);
  }
  LoginResponse response;
  response.email = user.email;
  response.token = token;
  return response;
}

IMPLEMENT_RPC(LogoutRPC, LogoutRequest, LogoutResponse) {
  if (request.token_uuid.empty()) {
    return TRACE(
        ValidationError("`token_uuid` must not be empty."));
  }
  SET_OR_RET(auto* ctx, GetContext());
  auto db = ctx->Get<IDatabaseClient>();
  SET_OR_RET(
      auto token,
      QueryView<Token>("tokens", db)
          .where(EQUALS(&Token::uuid, request.token_uuid))
          .first());

  token.active = false;
  OK_OR_RET(Upsert("tokens", token, *db));

  LogoutResponse response;
  response.logged_out = true;
  return response;
}
}  // namespace cs::apps::trycopilotai::api
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-2:8080 in 1ms.