forked from qvest-digital/loginsrv
/
auth.go
57 lines (46 loc) · 1 KB
/
auth.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package httpupstream
import (
"crypto/tls"
"net/http"
"net/url"
"time"
)
// Auth is the httpupstream authenticater
type Auth struct {
upstream *url.URL
skipverify bool
timeout time.Duration
}
// NewAuth creates an httpupstream authenticater
func NewAuth(upstream *url.URL, timeout time.Duration, skipverify bool) (*Auth, error) {
a := &Auth{
upstream: upstream,
skipverify: skipverify,
timeout: timeout,
}
return a, nil
}
// Authenticate the user
func (a *Auth) Authenticate(username, password string) (bool, error) {
c := &http.Client{
Timeout: a.timeout,
}
if a.upstream.Scheme == "https" && a.skipverify {
c.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
req, err := http.NewRequest("GET", a.upstream.String(), nil)
if err != nil {
return false, err
}
req.SetBasicAuth(username, password)
resp, err := c.Do(req)
if err != nil {
return false, err
}
if resp.StatusCode != 200 {
return false, nil
}
return true, nil
}