Can delete own aliases
This commit is contained in:
parent
e6a4271a75
commit
65d0d4a53e
3 changed files with 63 additions and 10 deletions
62
addy.go
62
addy.go
|
|
@ -66,25 +66,32 @@ func checkAddyApiAuthorization(authorization []byte) *string {
|
|||
return &username
|
||||
}
|
||||
|
||||
func addyAliasAPI(w http.ResponseWriter, r *http.Request) {
|
||||
func addyAliasAPIAuth(r *http.Request) (*string, error) {
|
||||
// Check authorization header
|
||||
fields := strings.Fields(r.Header.Get("Authorization"))
|
||||
if len(fields) != 2 || fields[0] != "Bearer" {
|
||||
http.Error(w, "Authorization header should be a valid Bearer token", http.StatusUnauthorized)
|
||||
return
|
||||
return nil, fmt.Errorf("Authorization header should be a valid Bearer token")
|
||||
}
|
||||
|
||||
// Decode header
|
||||
authorization, err := base32.StdEncoding.DecodeString(fields[1])
|
||||
if err != nil {
|
||||
log.Println("Invalid Authorization header: %s", err.Error())
|
||||
http.Error(w, "Authorization header should be a valid Bearer token", http.StatusUnauthorized)
|
||||
return
|
||||
log.Printf("Invalid Authorization header: %s", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
user := checkAddyApiAuthorization(authorization)
|
||||
if user == nil {
|
||||
http.Error(w, "Not authorized", http.StatusUnauthorized)
|
||||
return nil, fmt.Errorf("Not authorized")
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func addyAliasAPI(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := addyAliasAPIAuth(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -154,6 +161,47 @@ func addyAliasAPI(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
|
||||
func addyAliasAPIDelete(w http.ResponseWriter, r *http.Request) {
|
||||
user, err := addyAliasAPIAuth(r)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
email := r.PathValue("alias")
|
||||
|
||||
conn, err := myLDAP.Connect()
|
||||
if err != nil || conn == nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = conn.ServiceBind()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
dn, err := conn.SearchDN(*user, true)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = conn.DelMailAlias(dn, email)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Alias deleted for %s: %s", dn, email)
|
||||
http.Error(w, "", http.StatusOK)
|
||||
}
|
||||
|
||||
func generateRandomString(length int) string {
|
||||
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
result := make([]byte, length)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue