Home | History | Annotate | Download | only in auth
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chromeos/login/auth/user_context.h"
      6 #include "chromeos/login/user_names.h"
      7 
      8 namespace chromeos {
      9 
     10 UserContext::UserContext()
     11     : is_using_oauth_(true),
     12       auth_flow_(AUTH_FLOW_OFFLINE),
     13       user_type_(user_manager::USER_TYPE_REGULAR) {
     14 }
     15 
     16 UserContext::UserContext(const UserContext& other)
     17     : user_id_(other.user_id_),
     18       key_(other.key_),
     19       auth_code_(other.auth_code_),
     20       user_id_hash_(other.user_id_hash_),
     21       is_using_oauth_(other.is_using_oauth_),
     22       auth_flow_(other.auth_flow_),
     23       user_type_(other.user_type_),
     24       public_session_locale_(other.public_session_locale_),
     25       public_session_input_method_(other.public_session_input_method_) {
     26 }
     27 
     28 UserContext::UserContext(const std::string& user_id)
     29     : user_id_(login::CanonicalizeUserID(user_id)),
     30       is_using_oauth_(true),
     31       auth_flow_(AUTH_FLOW_OFFLINE),
     32       user_type_(user_manager::USER_TYPE_REGULAR) {
     33 }
     34 
     35 UserContext::UserContext(user_manager::UserType user_type,
     36                          const std::string& user_id)
     37     : is_using_oauth_(true),
     38       auth_flow_(AUTH_FLOW_OFFLINE),
     39       user_type_(user_type) {
     40   if (user_type_ == user_manager::USER_TYPE_REGULAR)
     41     user_id_ = login::CanonicalizeUserID(user_id);
     42   else
     43     user_id_ = user_id;
     44 }
     45 
     46 UserContext::~UserContext() {
     47 }
     48 
     49 bool UserContext::operator==(const UserContext& context) const {
     50   return context.user_id_ == user_id_ &&
     51          context.key_ == key_ &&
     52          context.auth_code_ == auth_code_ &&
     53          context.user_id_hash_ == user_id_hash_ &&
     54          context.is_using_oauth_ == is_using_oauth_ &&
     55          context.auth_flow_ == auth_flow_ &&
     56          context.user_type_ == user_type_ &&
     57          context.public_session_locale_ == public_session_locale_ &&
     58          context.public_session_input_method_ == public_session_input_method_;
     59 }
     60 
     61 bool UserContext::operator!=(const UserContext& context) const {
     62   return !(*this == context);
     63 }
     64 
     65 const std::string& UserContext::GetUserID() const {
     66   return user_id_;
     67 }
     68 
     69 const Key* UserContext::GetKey() const {
     70   return &key_;
     71 }
     72 
     73 Key* UserContext::GetKey() {
     74   return &key_;
     75 }
     76 
     77 const std::string& UserContext::GetAuthCode() const {
     78   return auth_code_;
     79 }
     80 
     81 const std::string& UserContext::GetUserIDHash() const {
     82   return user_id_hash_;
     83 }
     84 
     85 bool UserContext::IsUsingOAuth() const {
     86   return is_using_oauth_;
     87 }
     88 
     89 UserContext::AuthFlow UserContext::GetAuthFlow() const {
     90   return auth_flow_;
     91 }
     92 
     93 user_manager::UserType UserContext::GetUserType() const {
     94   return user_type_;
     95 }
     96 
     97 const std::string& UserContext::GetPublicSessionLocale() const {
     98   return public_session_locale_;
     99 }
    100 
    101 const std::string& UserContext::GetPublicSessionInputMethod() const {
    102   return public_session_input_method_;
    103 }
    104 
    105 bool UserContext::HasCredentials() const {
    106   return (!user_id_.empty() && !key_.GetSecret().empty()) ||
    107          !auth_code_.empty();
    108 }
    109 
    110 void UserContext::SetUserID(const std::string& user_id) {
    111   user_id_ = login::CanonicalizeUserID(user_id);
    112 }
    113 
    114 void UserContext::SetKey(const Key& key) {
    115   key_ = key;
    116 }
    117 
    118 void UserContext::SetAuthCode(const std::string& auth_code) {
    119   auth_code_ = auth_code;
    120 }
    121 
    122 void UserContext::SetUserIDHash(const std::string& user_id_hash) {
    123   user_id_hash_ = user_id_hash;
    124 }
    125 
    126 void UserContext::SetIsUsingOAuth(bool is_using_oauth) {
    127   is_using_oauth_ = is_using_oauth;
    128 }
    129 
    130 void UserContext::SetAuthFlow(AuthFlow auth_flow) {
    131   auth_flow_ = auth_flow;
    132 }
    133 
    134 void UserContext::SetUserType(user_manager::UserType user_type) {
    135   user_type_ = user_type;
    136 }
    137 
    138 void UserContext::SetPublicSessionLocale(const std::string& locale) {
    139   public_session_locale_ = locale;
    140 }
    141 
    142 void UserContext::SetPublicSessionInputMethod(const std::string& input_method) {
    143   public_session_input_method_ = input_method;
    144 }
    145 
    146 void UserContext::ClearSecrets() {
    147   key_.ClearSecret();
    148   auth_code_.clear();
    149 }
    150 
    151 }  // namespace chromeos
    152