forked from sonic-net/sonic-mgmt-framework
-
Notifications
You must be signed in to change notification settings - Fork 16
/
basicAuth.go
32 lines (24 loc) · 865 Bytes
/
basicAuth.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
package server
import (
"net/http"
"github.com/golang/glog"
)
func BasicAuthenAndAuthor(r *http.Request, rc *RequestContext) error {
username, passwd, authOK := r.BasicAuth()
if authOK == false {
glog.Errorf("[%s] User info not present", rc.ID)
return httpError(http.StatusUnauthorized, "")
}
glog.Infof("[%s] Received user=%s", rc.ID, username)
auth_success, err := UserPwAuth(username, passwd)
if auth_success == false {
glog.Infof("[%s] Failed to authenticate; %v", rc.ID, err)
return httpError(http.StatusUnauthorized, "")
}
if err := PopulateAuthStruct(username, &rc.Auth); err != nil {
glog.Infof("[%s] Failed to retrieve authentication information; %v", rc.ID, err)
return httpError(http.StatusUnauthorized, "")
}
glog.Infof("[%s] Authorization passed for user=%s, roles=%s", rc.ID, rc.Auth.User, rc.Auth.Roles)
return nil
}