-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (123 loc) · 3.69 KB
/
main.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
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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"net/http"
"github.com/gorilla/mux"
"metagit.org/fnordpipe/idm-api/modules/ldap"
ini "gopkg.in/ini.v1"
)
var cfg *ini.File
var tree ldap.Tree
type JsonAccount struct {
Username string `json:"username,omitempty"`
Surname string `json:"surname,omitempty"`
Password string `json:"password,omitempty"`
}
type JsonAccountResponse struct {
Username bool `json:"username"`
Password bool `json:"password"`
Surname bool `json:"surname"`
}
type JsonPassword struct {
NewPassword string `json:"new,omitempty"`
RepeatPassword string `json:"repeat,omitempty"`
OldPassword string `json:"old,omitempty"`
}
func ChangePassword(w http.ResponseWriter, r *http.Request) {
var passwords JsonPassword
params := mux.Vars(r)
_ = json.NewDecoder(r.Body).Decode(&passwords)
if passwords.OldPassword == "" || passwords.NewPassword == "" ||
passwords.NewPassword != passwords.RepeatPassword {
w.WriteHeader(http.StatusBadRequest)
return
}
_, err := tree.SearchAccount(params["username"])
if err == nil {
err = tree.ChangePassword(
params["username"],
passwords.NewPassword, passwords.OldPassword)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusNotFound)
}
func CreateAccount(w http.ResponseWriter, r *http.Request) {
var account JsonAccount
var ar = JsonAccountResponse{true, true, true}
_ = json.NewDecoder(r.Body).Decode(&account)
if account.Username == "" {
ar.Username = false
}
if account.Password == "" {
ar.Password = false
}
if account.Surname == "" {
ar.Surname = false
}
if !ar.Username || !ar.Password || !ar.Surname {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ar)
return
}
a, err := tree.SearchAccount(account.Username)
if err != nil {
a = ldap.Account{
CommonName: account.Username,
Surname: account.Surname,
Username: account.Username,
Password: account.Password,
}
err = tree.CreateAccount(a)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
} else {
w.WriteHeader(http.StatusCreated)
return
}
}
ar.Username = false
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(ar)
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "USAGE: %s <config>\n", os.Args[0])
os.Exit(1)
}
var err error
cfg, err = ini.Load(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read file %v\n", err)
os.Exit(2)
}
host := cfg.Section("server").Key("listen").MustString("127.0.0.1")
port := cfg.Section("server").Key("port").MustInt(5555)
ls := &ldap.Source{
Host: cfg.Section("ldap").Key("host").MustString("127.0.0.1"),
Port: cfg.Section("ldap").Key("port").MustInt(389),
BindDN: cfg.Section("ldap").Key("bind").String(),
BindPassword: cfg.Section("ldap").Key("password").String(),
}
tree = ldap.Tree{
Base: cfg.Section("ldap").Key("base").String(),
AttributeCommonName: cfg.Section("ldap").Key("cn").MustString("cn"),
AttributePassword: cfg.Section("ldap").Key("userpassword").MustString("userPassword"),
AttributeSurname: cfg.Section("ldap").Key("sn").MustString("sn"),
AttributeUsername: cfg.Section("ldap").Key("uid").MustString("uid"),
Filter: cfg.Section("ldap").Key("filter").String(),
Ls: ls,
}
router := mux.NewRouter()
router.HandleFunc("/account", CreateAccount).Methods("POST")
router.HandleFunc("/account/{username}/password", ChangePassword).Methods("POST")
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), router))
}