/
instance.go
145 lines (122 loc) · 3.91 KB
/
instance.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
package instance
import (
"context"
"fmt"
"net/http"
"regexp"
"time"
"github.com/google/go-github/github"
"github.com/kobsio/kobs/pkg/hub/auth"
"github.com/kobsio/kobs/pkg/hub/auth/jwt"
"github.com/mitchellh/mapstructure"
"golang.org/x/oauth2"
githuboauth "golang.org/x/oauth2/github"
)
var (
slugifyRe = regexp.MustCompile("[^a-z0-9]+")
)
// Config is the structure of the configuration for a single GitHub instance.
type Config struct {
Organization string `json:"organization"`
OAuth OAuthConfig `json:"oauth"`
Session auth.SessionConfig `json:"session"`
}
type OAuthConfig struct {
ClientID string `json:"clientID"`
ClientSecret string `json:"clientSecret"`
State string `json:"state"`
}
type Instance interface {
GetName() string
GetOrganization() string
TokenToCookie(token *oauth2.Token) (*http.Cookie, error)
TokenFromCookie(r *http.Request) (*oauth2.Token, error)
OAuthLoginURL() string
OAuthCallback(ctx context.Context, state, code string) (*oauth2.Token, *github.User, error)
OAuthIsAuthenticated(ctx context.Context, token *oauth2.Token) (*github.User, error)
}
type instance struct {
name string
config Config
oauthConfig *oauth2.Config
}
func (i *instance) GetName() string {
return i.name
}
func (i *instance) GetOrganization() string {
return i.config.Organization
}
// TokenToCookie returns a cookie for the given oauth token.
func (i *instance) TokenToCookie(token *oauth2.Token) (*http.Cookie, error) {
jwtToken, err := jwt.CreateToken(token, i.config.Session.Token, i.config.Session.ParsedInterval)
if err != nil {
return nil, err
}
return &http.Cookie{
Name: "kobs-plugin-github-" + i.name,
Value: jwtToken,
Path: "/",
Secure: true,
HttpOnly: true,
Expires: time.Now().Add(i.config.Session.ParsedInterval),
}, nil
}
// TokenFromCookie returns the token from the "kobs-oauth-github" cookie in the given request.
func (i *instance) TokenFromCookie(r *http.Request) (*oauth2.Token, error) {
cookie, err := r.Cookie("kobs-plugin-github-" + i.name)
if err != nil {
return nil, err
}
return jwt.ValidateToken[oauth2.Token](cookie.Value, i.config.Session.Token)
}
func (i *instance) OAuthLoginURL() string {
return i.oauthConfig.AuthCodeURL(i.config.OAuth.State, oauth2.AccessTypeOnline)
}
func (i *instance) OAuthCallback(ctx context.Context, state, code string) (*oauth2.Token, *github.User, error) {
if state != i.config.OAuth.State {
return nil, nil, fmt.Errorf("invalid oauth state, expected '%s', got '%s'", i.config.OAuth.State, state)
}
token, err := i.oauthConfig.Exchange(oauth2.NoContext, code)
if err != nil {
return nil, nil, err
}
oauthClient := i.oauthConfig.Client(oauth2.NoContext, token)
client := github.NewClient(oauthClient)
user, _, err := client.Users.Get(ctx, "")
if err != nil {
return nil, nil, err
}
return token, user, nil
}
func (i *instance) OAuthIsAuthenticated(ctx context.Context, token *oauth2.Token) (*github.User, error) {
oauthClient := i.oauthConfig.Client(oauth2.NoContext, token)
client := github.NewClient(oauthClient)
user, _, err := client.Users.Get(ctx, "")
if err != nil {
return nil, err
}
return user, nil
}
// New returns a new GitHub instance for the given configuration.
func New(name string, options map[string]any) (Instance, error) {
var config Config
err := mapstructure.Decode(options, &config)
if err != nil {
return nil, err
}
sessionInterval := time.Duration(48 * time.Hour)
if parsedSessionInterval, err := time.ParseDuration(config.Session.Interval); err == nil {
sessionInterval = parsedSessionInterval
}
config.Session.ParsedInterval = sessionInterval
return &instance{
name: name,
config: config,
oauthConfig: &oauth2.Config{
ClientID: config.OAuth.ClientID,
ClientSecret: config.OAuth.ClientSecret,
Scopes: []string{"user", "repo", "notifications", "project"},
Endpoint: githuboauth.Endpoint,
},
}, nil
}