/
server.go
179 lines (147 loc) · 3.85 KB
/
server.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package server
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"os/signal"
"syscall"
"time"
"github.com/JulienBreux/mksctl/internal/mksctl/config"
"github.com/coreos/go-oidc"
"github.com/pkg/browser"
"golang.org/x/oauth2"
)
const (
handlerPath = "/sso-callback"
handlerCodeQueryParam = "code"
readHeaderTimeout = 3 * time.Second
shutdownTimeout = 5 * time.Second
)
// Server represents a server interface
type Server interface {
Run() error
}
// New creates a new instance of server
func New() (Server, error) {
provider, err := oidc.NewProvider(context.Background(), issuerURL())
if err != nil {
return nil, err
}
oauth2Config := oauth2.Config{
ClientID: config.Config.AuthClientID,
RedirectURL: redirectURL(),
Endpoint: provider.Endpoint(),
}
srv := &http.Server{
Addr: serverAddress(),
ReadHeaderTimeout: readHeaderTimeout,
}
return &server{
interrupt: make(chan any, 1),
oauth2Config: oauth2Config,
server: srv,
}, nil
}
type server struct {
interrupt chan any
oauth2Config oauth2.Config
server *http.Server
}
// Run starts the server
func (s *server) Run() error {
if err := s.start(); err != nil {
return err
}
if err := openBrowser(authURL()); err != nil {
return err
}
return s.stop()
}
// start the callback server
func (s *server) start() error {
http.HandleFunc(handlerPath, s.handlerFunc)
go func() {
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
}()
return nil
}
// stop the callback server
func (s *server) stop() error {
stopFromOS(s.interrupt)
<-s.interrupt
ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
defer cancel()
return s.server.Shutdown(ctx)
}
// handlerFunc returns the callback handler
func (s *server) handlerFunc(w http.ResponseWriter, r *http.Request) {
// Check code query parametter
code := r.URL.Query().Get(handlerCodeQueryParam)
if code == "" {
w.WriteHeader(http.StatusNoContent)
fmt.Fprintf(w, "Code query parametter is missing.")
return
}
// Converts an authorization code into a token.
oauth2Token, err := s.oauth2Config.Exchange(r.Context(), code)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Unable to exchange the authorization code for a token.\n\nError: %v", err)
return
}
// Read access token
rawAccessToken, ok := oauth2Token.Extra("access_token").(string)
if !ok {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Access token not found.")
return
}
// Save access token to configuration
// TODO: Save to keyring
config.Config.AuthAccessToken = rawAccessToken
if err := config.Save(); err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Unable to save configuration file.")
return
}
w.WriteHeader(http.StatusAccepted)
fmt.Fprintf(w, "You can close this page. You're now authenticated.")
s.interrupt <- true
}
func authURL() string {
return fmt.Sprintf(
"%v/realms/%v/protocol/openid-connect/auth?client_id=%v&redirect_uri=%v&response_type=code",
config.Config.AuthServerURL,
config.Config.AuthClientRealm,
config.Config.AuthClientID,
url.QueryEscape(redirectURL()),
)
}
func issuerURL() string {
return fmt.Sprintf("%s/realms/%s", config.Config.AuthServerURL, config.Config.AuthClientRealm)
}
func serverAddress() string {
return fmt.Sprintf("localhost:%v", config.Config.AuthCallbackPort)
}
func redirectURL() string {
return fmt.Sprintf("http://%s/%s", serverAddress(), handlerPath)
}
func openBrowser(authURL string) error {
if err := browser.OpenURL(authURL); err != nil {
return err
}
fmt.Printf("Your browser has been opened to visit: \n\n\t%s\n\n", authURL)
return nil
}
func stopFromOS(stop chan any) {
stopOS := make(chan os.Signal, 1)
go func() {
<-stopOS
stop <- true
}()
signal.Notify(stopOS, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
}