-
Notifications
You must be signed in to change notification settings - Fork 20
/
auth.go
71 lines (56 loc) · 1.74 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package testutil
import (
"fmt"
"net"
"net/http"
oauth2 "github.com/oxisto/oauth2go"
"github.com/oxisto/oauth2go/login"
"golang.org/x/oauth2/clientcredentials"
)
const (
TestAuthUser = "clouditor"
TestAuthPassword = "clouditor"
TestAuthClientID = "client"
TestAuthClientSecret = "secret"
)
// StartAuthenticationServer starts an authentication server on a random port with
// users and clients specified in the TestAuthUser and TestAuthClientID constants.
func StartAuthenticationServer() (srv *oauth2.AuthorizationServer, port int, err error) {
var nl net.Listener
srv = oauth2.NewServer(":0",
oauth2.WithClient("cli", "", "http://localhost:10000/callback"),
oauth2.WithClient(TestAuthClientID, TestAuthClientSecret, ""),
login.WithLoginPage(
login.WithUser(TestAuthUser, TestAuthPassword),
login.WithBaseURL("/v1/auth"),
),
)
// simulate the /v1/auth endpoints
srv.Handler.(*http.ServeMux).Handle("/v1/auth/token", http.StripPrefix("/v1/auth", srv.Handler))
// create a new socket for HTTP communication
nl, err = net.Listen("tcp", srv.Addr)
if err != nil {
return nil, 0, fmt.Errorf("could not listen: %w", err)
}
go func() {
_ = srv.Serve(nl)
}()
port = nl.Addr().(*net.TCPAddr).Port
return srv, port, nil
}
func JWKSURL(port int) string {
return fmt.Sprintf("http://localhost:%d/.well-known/jwks.json", port)
}
func TokenURL(port int) string {
return fmt.Sprintf("http://localhost:%d/v1/auth/token", port)
}
func AuthURL(port int) string {
return fmt.Sprintf("http://localhost:%d/v1/auth/authorize", port)
}
func AuthClientConfig(port int) *clientcredentials.Config {
return &clientcredentials.Config{
ClientID: TestAuthClientID,
ClientSecret: TestAuthClientSecret,
TokenURL: TokenURL(port),
}
}