2411: Add copyright notices to everything.
[arvados.git] / sdk / go / auth / basic_auth_go13.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: Apache-2.0
4
5 // +build !go1.4
6
7 package auth
8
9 import (
10         "encoding/base64"
11         "net/http"
12         "strings"
13 )
14
15 func BasicAuth(r *http.Request) (username, password string, ok bool) {
16         tokens := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
17         if len(tokens) != 2 || tokens[0] != "Basic" {
18                 return "", "", false
19         }
20
21         decoded, err := base64.StdEncoding.DecodeString(tokens[1])
22         if err != nil {
23                 return "", "", false
24         }
25
26         userAndPass := strings.SplitN(string(decoded), ":", 2)
27         if len(userAndPass) != 2 {
28                 return "", "", false
29         }
30
31         return userAndPass[0], userAndPass[1], true
32 }