This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
loginserver.go
79 lines (65 loc) · 1.9 KB
/
loginserver.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
// Package loginserver implements a "terraform login protocol" server:
//
// https://developer.hashicorp.com/terraform/internals/v1.3.x/login-protocol#client
package loginserver
import (
"context"
"github.com/gorilla/mux"
"github.com/leg100/otf/internal/http/html"
"github.com/leg100/otf/internal/user"
)
const (
// OAuth2 client ID - purely advisory according to:
// https://developer.hashicorp.com/terraform/internals/v1.3.x/login-protocol#client
ClientID = "terraform"
AuthRoute = "/app/oauth2/auth"
TokenRoute = "/oauth2/token"
)
var Discovery = DiscoverySpec{
Client: ClientID,
GrantTypes: []string{"authz_code"},
Authz: AuthRoute,
Token: TokenRoute,
Ports: []int{10000, 10010},
}
type (
server struct {
secret []byte // for encrypting auth code
users userTokensClient // for creating user API token
html.Renderer // render consent UI
}
userTokensClient interface {
CreateToken(ctx context.Context, opts user.CreateUserTokenOptions) (*user.UserToken, []byte, error)
}
// Options for server constructor
Options struct {
Secret []byte // for encrypting auth code
UserService *user.Service
html.Renderer
}
authcode struct {
CodeChallenge string `json:"code_challenge"`
CodeChallengeMethod string `json:"code_challenge_method"`
Username string `json:"username"`
}
DiscoverySpec struct {
Client string `json:"client"`
GrantTypes []string `json:"grant_types"`
Authz string `json:"authz"`
Token string `json:"token"`
Ports []int `json:"ports"`
}
)
func NewServer(opts Options) *server {
return &server{
Renderer: opts.Renderer,
secret: opts.Secret,
users: opts.UserService,
}
}
func (s *server) AddHandlers(r *mux.Router) {
// authenticated
r.HandleFunc(AuthRoute, s.authHandler).Methods("GET", "POST")
// unauthenticated
r.HandleFunc(TokenRoute, s.tokenHandler).Methods("POST")
}