summaryrefslogtreecommitdiffstats
path: root/ios/web/web_state/js/credential_util.mm
blob: d0a9b4a1636d237f1651917ce8f9d85720332308 (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
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
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/web/web_state/js/credential_util.h"

#include "base/logging.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "ios/web/public/web_state/credential.h"
#include "url/gurl.h"

namespace {

// "type" value for a DictionaryValue representation of PasswordCredential.
const char* kPasswordCredentialType = "PasswordCredential";

// "type" value for a DictionaryValue representation of FederatedCredential.
const char* kFederatedCredentialType = "FederatedCredential";

}  // namespace

namespace web {

bool DictionaryValueToCredential(const base::DictionaryValue& value,
                                 Credential* credential) {
  DCHECK(credential);

  base::string16 type;
  if (!value.GetString("type", &type))
    return false;
  CredentialType credential_type;
  if (type == base::ASCIIToUTF16(kPasswordCredentialType))
    credential_type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
  else if (type == base::ASCIIToUTF16(kFederatedCredentialType))
    credential_type = CredentialType::CREDENTIAL_TYPE_FEDERATED;
  else
    return false;

  base::string16 id;
  if (!value.GetString("id", &id))
    return false;

  base::string16 name;
  value.GetString("name", &name);

  base::string16 avatar;
  GURL avatar_url;
  if (value.GetString("avatarURL", &avatar)) {
    avatar_url = GURL(avatar);
    if (!avatar_url.is_valid())
      return false;
  }

  base::string16 password;
  if (credential_type == CredentialType::CREDENTIAL_TYPE_PASSWORD &&
      !value.GetString("password", &password)) {
    return false;
  }

  base::string16 federation;
  GURL federation_url;
  if (credential_type == CredentialType::CREDENTIAL_TYPE_FEDERATED) {
    if (!value.GetString("federation", &federation))
      return false;
    federation_url = GURL(federation);
    if (!federation_url.is_valid())
      return false;
  }

  credential->type = credential_type;
  credential->id = id;
  credential->name = name;
  credential->avatar_url = avatar_url;
  credential->password = password;
  credential->federation_url = federation_url;
  return true;
}

void CredentialToDictionaryValue(const Credential& credential,
                                 base::DictionaryValue* value) {
  DCHECK(value);
  switch (credential.type) {
    case CredentialType::CREDENTIAL_TYPE_EMPTY:
      // Return an empty dictionary. This will cause "null" to be returned to
      // the JavaScript Promise resolver.
      value->Clear();
      return;
    case CredentialType::CREDENTIAL_TYPE_PASSWORD:
      value->SetString("type", kPasswordCredentialType);
      value->SetString("password", credential.password);
      break;
    case CredentialType::CREDENTIAL_TYPE_FEDERATED:
      value->SetString("type", kFederatedCredentialType);
      value->SetString("federation", credential.federation_url.spec());
      break;
    default:
      NOTREACHED();
  }
  value->SetString("id", credential.id);
  value->SetString("name", credential.name);
  value->SetString("avatarURL", credential.avatar_url.spec());
}

}  // web