forked from mystiq/dex
Merge pull request #147 from gyuho/use_stdpkg_basicauth
server: use standard lib http.Request.BasicAuth
This commit is contained in:
commit
8360f18fea
2 changed files with 1 additions and 26 deletions
|
@ -1,7 +1,6 @@
|
||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -29,30 +28,6 @@ func WriteError(w http.ResponseWriter, code int, msg string) {
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BasicAuth parses a username and password from the request's
|
|
||||||
// Authorization header. This was pulled from golang master:
|
|
||||||
// https://codereview.appspot.com/76540043
|
|
||||||
func BasicAuth(r *http.Request) (username, password string, ok bool) {
|
|
||||||
auth := r.Header.Get("Authorization")
|
|
||||||
if auth == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(auth, "Basic ") {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(auth, "Basic "))
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
cs := string(c)
|
|
||||||
s := strings.IndexByte(cs, ':')
|
|
||||||
if s < 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return cs[:s], cs[s+1:], true
|
|
||||||
}
|
|
||||||
|
|
||||||
func cacheControlMaxAge(hdr string) (time.Duration, bool, error) {
|
func cacheControlMaxAge(hdr string) (time.Duration, bool, error) {
|
||||||
for _, field := range strings.Split(hdr, ",") {
|
for _, field := range strings.Split(hdr, ",") {
|
||||||
parts := strings.SplitN(strings.TrimSpace(field), "=", 2)
|
parts := strings.SplitN(strings.TrimSpace(field), "=", 2)
|
||||||
|
|
|
@ -418,7 +418,7 @@ func handleTokenFunc(srv OIDCServer) http.HandlerFunc {
|
||||||
|
|
||||||
state := r.PostForm.Get("state")
|
state := r.PostForm.Get("state")
|
||||||
|
|
||||||
user, password, ok := phttp.BasicAuth(r)
|
user, password, ok := r.BasicAuth()
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Errorf("error parsing basic auth")
|
log.Errorf("error parsing basic auth")
|
||||||
writeTokenError(w, oauth2.NewError(oauth2.ErrorInvalidClient), state)
|
writeTokenError(w, oauth2.NewError(oauth2.ErrorInvalidClient), state)
|
||||||
|
|
Loading…
Reference in a new issue