@@ -22,16 +22,18 @@ import (
2222
2323const (
2424 DefaultConfigAuthPath = "/_auth"
25+ OneDayInHours = 24
2526)
2627
2728// Config the middleware configuration.
2829type Config struct {
29- ApiBaseUrl string `json:"api_base_url,omitempty"`
30- ApiSecretKey string `json:"api_secret_key,omitempty"`
31- AuthPath string `json:"auth_path,omitempty"`
32- JwtSecretKey string `json:"jwt_secret_key,omitempty"`
33- LogLevel string `json:"log_level,omitempty"`
34- Whitelist ConfigWhitelist `json:"whitelist,omitempty"`
30+ ApiBaseUrl string `json:"api_base_url,omitempty"`
31+ ApiSecretKey string `json:"api_secret_key,omitempty"`
32+ AuthPath string `json:"auth_path,omitempty"`
33+ JwtSecretKey string `json:"jwt_secret_key,omitempty"`
34+ JwtExpirationInHours int64 `json:"jwt_expiration_in_hours,omitempty"`
35+ LogLevel string `json:"log_level,omitempty"`
36+ Whitelist ConfigWhitelist `json:"whitelist,omitempty"`
3537}
3638
3739// ConfigWhitelist the middleware configuration whitelist.
@@ -47,13 +49,14 @@ type ConfigWhitelist struct {
4749 Teams []string `json:"teams,omitempty"`
4850}
4951
50- // CreateConfig creates the default middleware configuration.
52+ // CreateConfig creates the default middleware configuration. Required by Traefik.
5153func CreateConfig () * Config {
5254 return & Config {
53- ApiBaseUrl : "" ,
54- ApiSecretKey : "" ,
55- AuthPath : DefaultConfigAuthPath ,
56- JwtSecretKey : getRandomString32 (),
55+ ApiBaseUrl : "" ,
56+ ApiSecretKey : "" ,
57+ AuthPath : DefaultConfigAuthPath ,
58+ JwtSecretKey : getRandomString32 (),
59+ JwtExpirationInHours : OneDayInHours ,
5760 Whitelist : ConfigWhitelist {
5861 Ids : []string {},
5962 Logins : []string {},
@@ -73,6 +76,7 @@ type TraefikGithubOauthMiddleware struct {
7376 apiSecretKey string
7477 authPath string
7578 jwtSecretKey string
79+ jwtExpirationInHours int64
7680 whitelistIdSet * strset.Set
7781 whitelistLoginSet * strset.Set
7882 whitelistTeamSet * strset.Set
@@ -83,7 +87,7 @@ type TraefikGithubOauthMiddleware struct {
8387
8488var _ http.Handler = (* TraefikGithubOauthMiddleware )(nil )
8589
86- // New creates a new TraefikGithubOauthMiddleware.
90+ // New creates a new TraefikGithubOauthMiddleware. Required by Traefik.
8791func New (ctx context.Context , next http.Handler , config * Config , name string ) (http.Handler , error ) {
8892 logger := log .New (os .Stdout , "service=traefik-github-oauth-middleware level=debug msg=" , 0 )
8993 // endregion Setup logger
@@ -104,6 +108,7 @@ func New(ctx context.Context, next http.Handler, config *Config, name string) (h
104108 apiSecretKey : config .ApiSecretKey ,
105109 authPath : authPath ,
106110 jwtSecretKey : config .JwtSecretKey ,
111+ jwtExpirationInHours : config .JwtExpirationInHours ,
107112 whitelistIdSet : strset .New (config .Whitelist .Ids ... ),
108113 whitelistLoginSet : strset .New (config .Whitelist .Logins ... ),
109114 whitelistTeamSet : strset .New (config .Whitelist .Teams ... ),
@@ -132,6 +137,7 @@ func (middleware *TraefikGithubOauthMiddleware) handleRequest(rw http.ResponseWr
132137 if err != nil {
133138 if req .Method == http .MethodGet {
134139 middleware .redirectToOAuthPage (rw , req )
140+ return
135141 }
136142 middleware .logger .Printf ("Failed to get user from cookie: %s" , err .Error ())
137143 http .Error (rw , "" , http .StatusUnauthorized )
@@ -171,12 +177,15 @@ func (p TraefikGithubOauthMiddleware) handleAuthRequest(rw http.ResponseWriter,
171177 return
172178 }
173179
180+ exp := time .Now ().Add (time .Duration (p .jwtExpirationInHours ) * time .Hour )
181+
174182 // Generate JWTs
175183 tokenString , err := jwt .GenerateJwtTokenString (
176184 result .GitHubUserID ,
177185 result .GitHubUserLogin ,
178186 result .GithubTeamIDs ,
179187 p .jwtSecretKey ,
188+ exp ,
180189 )
181190 if err != nil {
182191 p .logger .Printf ("Failed to generate JWT: %s" , err .Error ())
@@ -187,6 +196,7 @@ func (p TraefikGithubOauthMiddleware) handleAuthRequest(rw http.ResponseWriter,
187196 Name : constant .COOKIE_NAME_JWT ,
188197 Value : tokenString ,
189198 HttpOnly : true ,
199+ Expires : exp ,
190200 })
191201 http .Redirect (rw , req , result .RedirectURI , http .StatusFound )
192202}
0 commit comments