summaryrefslogtreecommitdiff
path: root/test/elixir/test/proxyauth_test.exs
blob: 6f2d49a5314a295019c92f324a48a8e6ce71f299 (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
defmodule ProxyAuthTest do
  use CouchTestCase

  @moduletag :authentication

  @tag :with_db
  test "proxy auth with secret", context do
    db_name = context[:db_name]

    design_doc = %{
      _id: "_design/test",
      language: "javascript",
      shows: %{
        welcome: """
           function(doc,req) {
          return "Welcome " + req.userCtx["name"];
        }
        """,
        role: """
          function(doc, req) {
          return req.userCtx['roles'][0];
        }
        """
      }
    }

    {:ok, _} = create_doc(db_name, design_doc)

    users_db_name = random_db_name()
    create_db(users_db_name)

    secret = generate_secret(64)

    server_config = [
      %{
        :section => "chttpd_auth",
        :key => "authentication_db",
        :value => users_db_name
      },
      %{
        :section => "couch_httpd_auth",
        :key => "proxy_use_secret",
        :value => "true"
      },
      %{
        :section => "couch_httpd_auth",
        :key => "secret",
        :value => secret
      }
    ]

    run_on_modified_server(server_config, fn ->
      test_fun(db_name, users_db_name, secret)
    end)
    delete_db(users_db_name)
  end

  defp generate_secret(len) do
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
    |> String.splitter("", trim: true)
    |> Enum.take_random(len)
    |> Enum.join("")
  end

  defp hex_hmac_sha1(secret, message) do
    signature = :crypto.hmac(:sha, secret, message)
    Base.encode16(signature, case: :lower)
  end

  def test_fun(db_name, users_db_name, secret) do
    user = prepare_user_doc(name: "couch@apache.org", password: "test")
    create_doc(users_db_name, user)

    resp =
      Couch.get("/_session",
        headers: [authorization: "Basic Y291Y2hAYXBhY2hlLm9yZzp0ZXN0"]
      )

    assert resp.body["userCtx"]["name"] == "couch@apache.org"
    assert resp.body["info"]["authenticated"] == "default"

    headers = [
      "X-Auth-CouchDB-UserName": "couch@apache.org",
      "X-Auth-CouchDB-Roles": "test",
      "X-Auth-CouchDB-Token": hex_hmac_sha1(secret, "couch@apache.org")
    ]
    resp = Couch.get("/#{db_name}/_design/test/_show/welcome", headers: headers)
    assert resp.body == "Welcome couch@apache.org"

    resp = Couch.get("/#{db_name}/_design/test/_show/role", headers: headers)
    assert resp.body == "test"
  end

  @tag :with_db
  test "proxy auth without secret", context do
    db_name = context[:db_name]

    design_doc = %{
      _id: "_design/test",
      language: "javascript",
      shows: %{
        welcome: """
           function(doc,req) {
          return "Welcome " + req.userCtx["name"];
        }
        """,
        role: """
          function(doc, req) {
          return req.userCtx['roles'][0];
        }
        """
      }
    }

    {:ok, _} = create_doc(db_name, design_doc)

    users_db_name = random_db_name()
    create_db(users_db_name)

    server_config = [
      %{
        :section => "chttpd_auth",
        :key => "authentication_db",
        :value => users_db_name
      },
      %{
        :section => "couch_httpd_auth",
        :key => "proxy_use_secret",
        :value => "false"
      }
    ]

    run_on_modified_server(server_config, fn ->
      test_fun_no_secret(db_name, users_db_name)
    end)

    delete_db(users_db_name)
  end

  def test_fun_no_secret(db_name, users_db_name) do
    user = prepare_user_doc(name: "couch@apache.org", password: "test")
    create_doc(users_db_name, user)

    resp =
      Couch.get("/_session",
        headers: [authorization: "Basic Y291Y2hAYXBhY2hlLm9yZzp0ZXN0"]
      )

    assert resp.body["userCtx"]["name"] == "couch@apache.org"
    assert resp.body["info"]["authenticated"] == "default"

    headers = [
      "X-Auth-CouchDB-UserName": "couch@apache.org",
      "X-Auth-CouchDB-Roles": "test"
    ]

    resp = Couch.get("/#{db_name}/_design/test/_show/welcome", headers: headers)
    assert resp.body == "Welcome couch@apache.org"

    resp = Couch.get("/#{db_name}/_design/test/_show/role", headers: headers)
    assert resp.body == "test"
  end
end