forked from laincloud/lvault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
51 lines (46 loc) · 1.47 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
package main
import (
"io/ioutil"
"net/http"
"github.com/mijia/sweb/log"
"golang.org/x/net/context"
)
const SCOPE = ""
func (l *Lvault) auth(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
r.ParseForm()
redirect_uri := r.Form.Get("redirect_uri")
response_type := r.Form.Get("response_type")
state := r.Form.Get("state")
client_id := l.ClientId
scope := SCOPE
url := l.SSOSite + r.URL.Path + "?response_type=" + response_type + "&redirect_uri=" + redirect_uri + "&client_id=" + client_id + "&scope=" + scope + "&state=" + state
log.Debug(url)
w.Header().Set("Location", url)
w.WriteHeader(http.StatusSeeOther)
return ctx
}
func (l *Lvault) token(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
r.ParseForm()
redirect_uri := r.Form.Get("redirect_uri")
code := r.Form.Get("code")
grant_type := r.Form.Get("grant_type")
client_id := l.ClientId
client_secret := l.ClientSecret
url := l.SSOSite + r.URL.Path + "?grant_type=" + grant_type + "&redirect_uri=" + redirect_uri + "&client_id=" + client_id + "&code=" + code + "&client_secret=" + client_secret
log.Debug(url)
req, _ := http.NewRequest(r.Method, url, nil)
resp, err := http.DefaultClient.Do(req)
if err == nil {
defer resp.Body.Close()
} else {
log.Error(err)
w.WriteHeader(http.StatusServiceUnavailable)
return ctx
}
log.Debug(resp)
b, _ := ioutil.ReadAll(resp.Body)
log.Debug(string(b))
w.WriteHeader(resp.StatusCode)
w.Write(b)
return ctx
}