5824: Move request auth code into an SDK package. Support more ways of passing tokens.
[arvados.git] / sdk / go / auth / basic_auth_go13.go
1 // +build !go1.4
2
3 package auth
4
5 import (
6         "encoding/base64"
7         "net/http"
8         "strings"
9 )
10
11 func BasicAuth(r *http.Request) (username, password string, ok bool) {
12         tokens := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
13         if len(tokens) != 2 || tokens[0] != "Basic" {
14                 return "", "", false
15         }
16
17         decoded, err := base64.StdEncoding.DecodeString(tokens[1])
18         if err != nil {
19                 return "", "", false
20         }
21
22         userAndPass := strings.SplitN(string(decoded), ":", 2)
23         if len(userAndPass) != 2 {
24                 return "", "", false
25         }
26
27         return userAndPass[0], userAndPass[1], true
28 }