-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth.go
151 lines (126 loc) · 3.61 KB
/
oauth.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package multivers
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/oauth2"
)
type LinkHandler func(*url.URL) error
type AuthorizationHandler func(string) error
type TokenHandler func(*oauth2.Token) error
const (
localPort = "9090"
authorizationCallbackPath = "/authorization/callback"
scope = "http://UNIT4.Multivers.API/Web/WebApi/*"
oauthStateString = ""
authorizationTimeout = 60 * time.Second
tokenTimeout = 5 * time.Second
)
func NewOauth2Config(baseURL *url.URL) *oauth2.Config {
// Strip trailing slash
baseURL.Path = strings.TrimSuffix(baseURL.Path, "/")
// These are not registered in the oauth library by default
oauth2.RegisterBrokenAuthHeaderProvider(baseURL.String())
return &oauth2.Config{
RedirectURL: getAuthorizationCallbackURL(),
ClientID: "",
ClientSecret: "",
Scopes: []string{scope},
Endpoint: oauth2.Endpoint{
AuthURL: baseURL.String() + "/OAuth/Authorize",
TokenURL: baseURL.String() + "/OAuth/Token",
},
}
}
func getAuthorizationCallbackURL() string {
return fmt.Sprintf("http://localhost:%s%s", localPort, authorizationCallbackPath)
}
func GetNewOauth2Token(oauthConfig *oauth2.Config, linkCallback LinkHandler, authorizationCallback AuthorizationHandler, tokenCallback TokenHandler) (*oauth2.Token, error) {
url, err := generateLoginLink(oauthConfig)
if err != nil {
return nil, err
}
// if linkHandler is provided execute it
if linkCallback != nil {
err = linkCallback(url)
if err != nil {
return nil, err
}
}
authorizationCode, err := waitForAuthorizationCallback()
if err != nil {
return nil, err
}
// if authorizationHandler is provided execute it
if authorizationCallback != nil {
authorizationCallback(authorizationCode)
}
token, err := getOauth2Token(oauthConfig, authorizationCode)
if err != nil {
return nil, err
}
if tokenCallback != nil {
err := tokenCallback(token)
if err != nil {
return nil, err
}
}
return token, nil
}
func generateLoginLink(oauthConfig *oauth2.Config) (*url.URL, error) {
u := oauthConfig.AuthCodeURL(oauthStateString)
return url.Parse(u)
}
// @TODO: add context so the server can be canceled?
func waitForAuthorizationCallback() (string, error) {
mux := http.NewServeMux()
ac := newAuthorizationController()
mux.HandleFunc("/authorization/callback", ac.callbackHandler)
srv := &http.Server{Addr: ":" + localPort, Handler: mux}
go func() {
// service connections
srv.ListenAndServe()
}()
// block until data is received on one of the channels
var code string
var err error
select {
case <-time.After(authorizationTimeout):
err = errors.New("Timed out waiting for authorization callback")
case code = <-ac.resultCh:
case err = <-ac.errCh:
}
close(ac.errCh)
close(ac.resultCh)
return code, err
}
func newAuthorizationController() *authorizationController {
ac := &authorizationController{}
ac.errCh = make(chan error)
ac.resultCh = make(chan string)
return ac
}
type authorizationController struct {
errCh chan error
resultCh chan string
}
func (c *authorizationController) callbackHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
code := r.URL.Query().Get("code")
if code == "" {
log.Println("sending on error channel")
c.errCh <- fmt.Errorf("No code found in query parameters")
return
}
log.Println("sending on result channel")
c.resultCh <- code
}
func getOauth2Token(oauthConfig *oauth2.Config, code string) (*oauth2.Token, error) {
ctx, _ := context.WithTimeout(context.Background(), tokenTimeout)
return oauthConfig.Exchange(ctx, code)
}