Initial commit
This commit is contained in:
38
authorization.go
Normal file
38
authorization.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package header
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gopatchy/jsrest"
|
||||
)
|
||||
|
||||
func ParseAuthorization(r *http.Request) (string, string) {
|
||||
auth := r.Header.Get("Authorization")
|
||||
|
||||
if auth == "" {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
parts := strings.Split(auth, " ")
|
||||
if len(parts) != 2 {
|
||||
return "", ""
|
||||
}
|
||||
|
||||
return parts[0], parts[1]
|
||||
}
|
||||
|
||||
func ParseBasic(val string) (string, string, error) {
|
||||
raw, err := base64.StdEncoding.DecodeString(val)
|
||||
if err != nil {
|
||||
return "", "", jsrest.Errorf(jsrest.ErrBadRequest, "Authorization header Basic data base64 decode failed (%w)", err)
|
||||
}
|
||||
|
||||
parts := strings.SplitN(string(raw), ":", 2)
|
||||
if len(parts) != 2 {
|
||||
return "", "", jsrest.Errorf(jsrest.ErrBadRequest, "Authorization header Basic data malformed")
|
||||
}
|
||||
|
||||
return parts[0], parts[1], nil
|
||||
}
|
||||
Reference in New Issue
Block a user