This repository has been archived by the owner on Jun 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
externuserdb.go
120 lines (101 loc) · 3.28 KB
/
externuserdb.go
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
// externuserdb.go - extern REST API backed Katzenpost server user database.
// Copyright (C) 2017 Kali Kaneko.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// Package externuserdb implements the Katzenpost server user database with
// http calls to a external authorization source (expected to run in localhost).
package externuserdb
import (
"errors"
"net/http"
"net/url"
"encoding/hex"
"github.com/katzenpost/core/crypto/ecdh"
"github.com/katzenpost/server/userdb"
"github.com/ugorji/go/codec"
)
var (
errCantModify = errors.New("Not implemented: External authentication is enabled, you can not modify users")
errNotSupported = errors.New("Not implemented: Support not implemented yet")
jsonHandle = &codec.JsonHandle{}
)
type externAuth struct {
provider string
}
func (e *externAuth) doPost(endpoint string, data url.Values) bool {
uri := e.provider + "/" + endpoint
rsp, err := http.PostForm(uri, data)
if err != nil {
return false
}
defer rsp.Body.Close()
response := map[string]bool{}
d := codec.NewDecoder(rsp.Body, jsonHandle)
if err = d.Decode(&response); err != nil {
return false
}
return rsp.StatusCode == 200 && response[endpoint]
}
func (e *externAuth) IsValid(u []byte, k *ecdh.PublicKey) bool {
form := url.Values{"user": {string(u)}, "key": {k.String()}}
return e.doPost("isvalid", form)
}
func (e *externAuth) Exists(u []byte) bool {
form := url.Values{"user": {string(u)}}
return e.doPost("exists", form)
}
func (e *externAuth) Add(u []byte, k *ecdh.PublicKey, update bool) error {
return errCantModify
}
func (e *externAuth) Link(u []byte) (*ecdh.PublicKey, error) {
return nil, errNotSupported
}
func (e *externAuth) SetIdentity(u []byte, k *ecdh.PublicKey) error {
return errNotSupported
}
func (e *externAuth) Identity(u []byte) (*ecdh.PublicKey, error) {
endpoint := "getidkey"
uri := e.provider + "/" + endpoint
form := url.Values{"user": {string(u)}}
rsp, err := http.PostForm(uri, form)
if err != nil {
return nil, err
}
defer rsp.Body.Close()
if rsp.StatusCode == 200 {
response := map[string]string{}
d := codec.NewDecoder(rsp.Body, jsonHandle)
if err = d.Decode(&response); err != nil {
return nil, err
}
if pkhex, ok := response[endpoint]; ok {
if decoded, err := hex.DecodeString(pkhex); err == nil {
pk := new(ecdh.PublicKey)
if err := pk.FromBytes(decoded); err == nil {
return pk, nil
}
}
}
}
return nil, userdb.ErrNoIdentity
}
func (e *externAuth) Remove(u []byte) error {
return errCantModify
}
func (e *externAuth) Close() {
}
// New creates an external user database with the given provider
func New(provider string) (userdb.UserDB, error) {
return &externAuth{provider}, nil
}