-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.go
95 lines (79 loc) · 2.14 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"reflect"
"strconv"
plugin "github.com/csweichel/werft/pkg/plugin/client"
"github.com/csweichel/werft/pkg/plugin/common"
"golang.org/x/oauth2"
"github.com/google/go-github/v43/github"
)
// Config configures this plugin
type Config struct {
EnableTeams bool `yaml:"enableTeams,omitempty"`
EnableEmails bool `yaml:"enableEmails,omitempty"`
}
func main() {
plugin.Serve(&Config{},
plugin.WithAuthenticationPlugin(&githubAuthPlugin{}),
)
fmt.Fprintln(os.Stderr, "shutting down")
}
type githubAuthPlugin struct{}
func (*githubAuthPlugin) Run(ctx context.Context, config interface{}) (common.AuthenticationPluginServer, error) {
cfg, ok := config.(*Config)
if !ok {
return nil, fmt.Errorf("config has wrong type %s", reflect.TypeOf(config))
}
return &authServer{Config: cfg}, nil
}
type authServer struct {
Config *Config
}
func (as *authServer) Authenticate(ctx context.Context, req *common.AuthenticateRequest) (*common.AuthenticateResponse, error) {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: req.Token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
ghreq, err := client.NewRequest("GET", "user", nil)
if err != nil {
return nil, err
}
user := new(github.User)
resp, err := client.Do(ctx, ghreq, user)
if resp.StatusCode == http.StatusUnauthorized {
return &common.AuthenticateResponse{Known: false}, nil
}
if err != nil {
return nil, err
}
var emails []string
if as.Config.EnableEmails {
rawEmails, _, _ := client.Users.ListEmails(ctx, nil)
for _, e := range rawEmails {
if !e.GetVerified() {
continue
}
emails = append(emails, e.GetEmail())
}
}
var teams []string
if as.Config.EnableTeams {
rawTeams, _, _ := client.Teams.ListUserTeams(ctx, nil)
for _, t := range rawTeams {
teams = append(teams, t.GetURL())
}
}
return &common.AuthenticateResponse{
Known: true,
Username: user.GetLogin(),
Metadata: map[string]string{
"two-factor-authentication": strconv.FormatBool(user.GetTwoFactorAuthentication()),
"name": user.GetName(),
},
Emails: emails,
Teams: teams,
}, nil
}