Skip to content

Commit a33a9cd

Browse files
authored
feat: middleware function (#3)
* feat: middleware function * fix: error not err as a type * fix: add logging and return * feat: readme updates
1 parent f676596 commit a33a9cd

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,21 @@ csh.Init(
2929

3030
```
3131
r.GET("/auth/login", csh.AuthRequest) // This endpoint should match auth_uri
32-
r.GET("/auth/redir", csh.AuthCallback) // This endpoint should match the relative portion of redirect_uri
33-
r.Get("/auth/logout", csh.AuthLogout)
32+
r.GET("/auth/callback", csh.AuthCallback) // This endpoint should match the relative portion of redirect_uri
33+
r.GET("/auth/logout", csh.AuthLogout)
3434
```
3535

3636
4. Add endpoints to be behind authentication
3737

38+
a. Use a wrapper function
3839
```
39-
r.Get("/hidden/prize", csh.AuthWrapper(endpoint_hidden_prize))
40+
r.GET("/hidden/prize", csh.AuthWrapper(endpoint_hidden_prize))
4041
```
42+
43+
b. Use middleware.
44+
45+
For a single route: `r.GET("/hidden/prize", csh.AuthWrapper, endpoint_hidden_prize)`
46+
This works because Gin will run the widest scope function to the most narrow scope function, in order.
47+
48+
For more/all routes: Check the [Gin Middleware documentation](https://gin-gonic.com/en/docs/middleware/) page.
49+

csh_auth.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ type CSHUserInfo struct {
5656
// auth helper
5757
// =================
5858

59-
func (auth *CSHAuth) AuthWrapper(page gin.HandlerFunc) gin.HandlerFunc {
60-
return gin.HandlerFunc(func(c *gin.Context) {
59+
func (auth *CSHAuth) addAuthUserInfoContext(c *gin.Context) error {
6160
cookie, err := c.Cookie(CookieName)
6261
if err != nil || cookie == "" {
6362
log.Info("cookie not found")
6463
c.Redirect(http.StatusFound, auth.authenticate_uri+"?referer="+c.Request.URL.String())
65-
return
64+
return errors.New("cookie not found")
6665
}
6766

6867
token, err := jwt.ParseWithClaims(cookie, &CSHClaims{}, func(token *jwt.Token) (interface{}, error) {
@@ -73,20 +72,39 @@ func (auth *CSHAuth) AuthWrapper(page gin.HandlerFunc) gin.HandlerFunc {
7372
})
7473
if err != nil {
7574
log.Error("token failure")
76-
return
75+
return errors.New("token failure")
7776
}
7877

7978
if claims, ok := token.Claims.(*CSHClaims); ok && token.Valid {
8079
// add in user info data
8180
c.Set(AuthKey, *claims)
82-
// call the wrapped func
83-
page(c)
8481
} else {
8582
log.Error("claim parsing failure")
83+
return errors.New("failure parsing claims from token")
8684
}
85+
return nil
86+
}
87+
88+
func (auth *CSHAuth) AuthWrapper(page gin.HandlerFunc) gin.HandlerFunc {
89+
return gin.HandlerFunc(func(c *gin.Context) {
90+
err := auth.addAuthUserInfoContext(c)
91+
if err != nil {
92+
return
93+
}
94+
page(c)
8795
})
8896
}
8997

98+
func (auth *CSHAuth) AuthMiddleware() gin.HandlerFunc {
99+
return func(c *gin.Context) {
100+
err := auth.addAuthUserInfoContext(c)
101+
if err != nil {
102+
return
103+
}
104+
c.Next()
105+
}
106+
}
107+
90108
func (auth *CSHAuth) AuthRequest(c *gin.Context) {
91109
// Thrash this so we don't get additive weirdness
92110
auth.config.RedirectURL = auth.redirect_uri + "?referer=" + c.Query("referer")
@@ -130,7 +148,7 @@ func (auth *CSHAuth) AuthCallback(c *gin.Context) {
130148
c.Redirect(http.StatusFound, c.Query("referer"))
131149
}
132150

133-
func (auth *CSHAuth) Init(clientID, clientSecret, secret, state, server_host, redirect_uri, auth_uri string, scopes []string) {
151+
func (auth *CSHAuth) Init(clientID, clientSecret, secret, state, server_host, redirect_uri, auth_uri string, scopes []string) error {
134152
auth.clientID = clientID
135153
auth.clientSecret = clientSecret
136154
auth.secret = secret
@@ -144,6 +162,8 @@ func (auth *CSHAuth) Init(clientID, clientSecret, secret, state, server_host, re
144162
auth.provider, err = oidc.NewProvider(auth.ctx, ProviderURI)
145163
if err != nil {
146164
log.Error("Failed to Create oidc Provider")
165+
log.Error(err)
166+
return err
147167
}
148168
copy(scopes[:], []string{oidc.ScopeOpenID}[:])
149169
log.Info(auth.authenticate_uri)
@@ -154,6 +174,7 @@ func (auth *CSHAuth) Init(clientID, clientSecret, secret, state, server_host, re
154174
RedirectURL: auth.redirect_uri,
155175
Scopes: scopes,
156176
}
177+
return nil
157178
}
158179

159180
func (auth *CSHAuth) AuthLogout(c *gin.Context) {

0 commit comments

Comments
 (0)