-
Notifications
You must be signed in to change notification settings - Fork 67
/
oauth.go
161 lines (130 loc) 路 3.55 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
152
153
154
155
156
157
158
159
160
161
package oauth
import (
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"sync"
"github.com/kubeshop/tracetest/cli/ui"
)
type OnAuthSuccess func(token string, jwt string)
type OnAuthFailure func(err error)
type OAuthServer struct {
endpoint string
frontendEndpoint string
onSuccess OnAuthSuccess
onFailure OnAuthFailure
port int
server *http.Server
mutex sync.Mutex
}
type Option func(*OAuthServer)
func NewOAuthServer(endpoint, frontendEndpoint string) *OAuthServer {
return &OAuthServer{
endpoint: endpoint,
frontendEndpoint: frontendEndpoint,
}
}
func (s *OAuthServer) WithOnSuccess(onSuccess OnAuthSuccess) *OAuthServer {
s.onSuccess = onSuccess
return s
}
func (s *OAuthServer) WithOnFailure(onFailure OnAuthFailure) *OAuthServer {
s.onFailure = onFailure
return s
}
func (s *OAuthServer) GetAuthJWT() error {
url, err := s.getUrl()
if err != nil {
return fmt.Errorf("failed to start oauth server: %w", err)
}
loginUrl := fmt.Sprintf("%soauth?callback=%s", s.frontendEndpoint, url)
ui := ui.DefaultUI
err = ui.OpenBrowser(loginUrl)
if err != nil {
return fmt.Errorf("failed to open the oauth url: %s", loginUrl)
}
return s.start()
}
type JWTResponse struct {
Jwt string `json:"jwt"`
}
func (s *OAuthServer) ExchangeToken(token string) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/tokens/%s/exchange", s.endpoint, token), nil)
if err != nil {
return "", fmt.Errorf("failed to create request: %w", err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("failed to exchange token: %w", err)
}
if res.StatusCode != http.StatusCreated {
return "", fmt.Errorf("failed to exchange token: %s", res.Status)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
var jwtResponse JWTResponse
err = json.Unmarshal(body, &jwtResponse)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response body: %w", err)
}
return jwtResponse.Jwt, nil
}
func (s *OAuthServer) getUrl() (string, error) {
port, err := getFreePort()
if err != nil {
return "", fmt.Errorf("failed to get free port: %w", err)
}
s.port = port
return fmt.Sprintf("http://localhost:%d", port), nil
}
func (s *OAuthServer) start() error {
srv := &http.Server{Addr: fmt.Sprintf(":%d", s.port)}
s.server = srv
http.HandleFunc("/", s.callback)
return srv.ListenAndServe()
}
func (s *OAuthServer) callback(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", strings.TrimSuffix(s.frontendEndpoint, "/"))
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"success": true}`))
go s.handleResult(r)
}
func (s *OAuthServer) handleResult(r *http.Request) {
s.mutex.Lock()
defer s.mutex.Unlock()
tokenId := r.URL.Query().Get("tokenId")
if tokenId == "" {
s.onFailure(fmt.Errorf("tokenId not found"))
return
}
jwt, err := s.ExchangeToken(tokenId)
if err != nil {
s.onFailure(err)
return
}
s.onSuccess(tokenId, jwt)
err = s.server.Shutdown(context.Background())
if err != nil {
s.onFailure(fmt.Errorf("failed to shutdown oauth server: %w", err))
return
}
}
func getFreePort() (port int, err error) {
var a *net.TCPAddr
if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil {
var l *net.TCPListener
if l, err = net.ListenTCP("tcp", a); err == nil {
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}
}
return
}