Skip to content

Commit 5f93858

Browse files
committed
feat: v1 readme, refactored some names
1 parent 8c3a705 commit 5f93858

2 files changed

Lines changed: 39 additions & 42 deletions

File tree

README.md

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,36 @@ An @ComputerScienceHouse authentication wrapper for Gin.
55

66
## Usage
77

8-
1. Create a CSHAuth Struct
9-
10-
```
11-
csh := csh_auth.CSHAuth{}
12-
```
13-
14-
2. Initialize your CSHAuth object
15-
16-
```
17-
csh.Init(
18-
/* oidc_client_id */, // The OIDC client ID
19-
/* oidc_client_secret */, // The OIDC client Secret
20-
/* jwt_secret */, // I just used a random sequence of > 16 characters
21-
/* state */, // I just used a random sequence of > 16 characters
22-
/* server_host */, // The domain your application will run from
23-
/* redirect_uri */, // The OIDC redirect URI
24-
/* auth_uri */, // The relative path for your authentication endpoint
8+
1. Initialize your csh-auth object
9+
10+
```
11+
auth := csh-auth.Init(
12+
clientID // the OIDC client ID
13+
clientSecret // the OIDC client secret
14+
serverURL // the "base" URL that this service is hosted from, e.g. "http://localhost:8000"
15+
loginURL // the URL for users to start the OAuth flow and login.
16+
// Commonly, this is set to something like ServerHost+"/auth/login"
17+
callbackURL // the URL that users will be redirected to at the end of the OAuth flow.
18+
// Commonly, this is set to something like ServerHost+"/auth/callback"
19+
scopes // pick scopes the application will use
2520
)
2621
```
2722

28-
3. Add required CSHAuth endpoints
23+
2. Add csh-auth endpoints for user login
2924

3025
```
31-
r.GET("/auth/login", csh.AuthRequest) // This endpoint should match auth_uri
32-
r.GET("/auth/callback", csh.AuthCallback) // This endpoint should match the relative portion of redirect_uri
33-
r.GET("/auth/logout", csh.AuthLogout)
26+
r.GET("/auth/login", auth.HandleLogin) // This endpoint should match the path for loginURL
27+
r.GET("/auth/callback", auth.HandleCallback) // This endpoint should match the path for callbackURL
28+
r.GET("/auth/logout", auth.HandleLogout)
3429
```
3530

36-
4. Add endpoints to be behind authentication
37-
38-
a. Use a wrapper function
39-
```
40-
r.GET("/hidden/prize", csh.AuthWrapper(endpoint_hidden_prize))
41-
```
31+
3. Add endpoints to be behind authentication
4232

43-
b. Use middleware.
33+
For client authentication, use `auth.CookieMiddleware()`
34+
For application authentication via Bearer tokens, use `auth.HeaderMiddleware()`.
35+
The HeaderMiddleware only accepts the `Authorization` header with the format `Bearer: <JWT AccessToken>`.
4436

45-
For a single route: `r.GET("/hidden/prize", csh.AuthWrapper, endpoint_hidden_prize)`
37+
For a single route: `r.GET("/locked/prize", auth.CookieMiddleware(), endpoint_hidden_prize)`
4638
This works because Gin will run the widest scope function to the most narrow scope function, in order.
4739

4840
For more/all routes: Check the [Gin Middleware documentation](https://gin-gonic.com/en/docs/middleware/) page.

auth.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ type Auth struct {
2929
clientSecret string
3030
// serverURL is the "base" URL that this service is hosted from, e.g. "http://localhost:8000"
3131
serverURL string
32-
// authenticateURL is the URL for users to start the OAuth flow and login.
32+
// loginURL is the URL for users to start the OAuth flow and login.
3333
// Commonly, this is set to something like ServerHost+"/auth/login"
34-
authenticateURL string
34+
loginURL string
3535
// callbackURL is the URL that users will be redirected to at the end of the OAuth flow.
3636
// Commonly, this is set to something like ServerHost+"/auth/callback"
3737
callbackURL string
@@ -56,14 +56,14 @@ type Claims struct {
5656
UserInfo
5757
}
5858

59-
func Init(oidcClientID string, oidcClientSecret string, serverURL string, authenticateURL string, callbackURL string, scopes []string) (Auth, error) {
59+
func Init(oidcClientID string, oidcClientSecret string, serverURL string, loginURL string, callbackURL string, scopes []string) (Auth, error) {
6060
auth := Auth{
61-
clientID: oidcClientID,
62-
clientSecret: oidcClientSecret,
63-
serverURL: serverURL,
64-
authenticateURL: authenticateURL,
65-
callbackURL: callbackURL,
66-
ctx: context.Background(),
61+
clientID: oidcClientID,
62+
clientSecret: oidcClientSecret,
63+
serverURL: serverURL,
64+
loginURL: loginURL,
65+
callbackURL: callbackURL,
66+
ctx: context.Background(),
6767
}
6868

6969
auth.secure = serverURL[0:5] == "https"
@@ -106,18 +106,18 @@ func (auth *Auth) HandleCallback(c *gin.Context) {
106106
ref, err := c.Cookie("ref")
107107
if err != nil {
108108
log.Error("no callback ref cookie")
109-
c.Redirect(http.StatusFound, auth.authenticateURL)
109+
c.Redirect(http.StatusFound, auth.loginURL)
110110
return
111111
}
112112
state, ok := StateLookup[ref]
113113
if !ok {
114114
log.Error("callback ref not found")
115-
c.Redirect(http.StatusFound, auth.authenticateURL)
115+
c.Redirect(http.StatusFound, auth.loginURL)
116116
return
117117
}
118118
if c.Query("state") != state {
119119
log.Error("state does not match")
120-
c.Redirect(http.StatusFound, auth.authenticateURL)
120+
c.Redirect(http.StatusFound, auth.loginURL)
121121
return
122122
}
123123

@@ -131,14 +131,19 @@ func (auth *Auth) HandleCallback(c *gin.Context) {
131131
c.Redirect(http.StatusFound, c.Query("referer"))
132132
}
133133

134+
func (auth *Auth) HandleLogout(c *gin.Context) {
135+
c.SetCookie(CookieName, "", 0, "", "", false, true)
136+
c.Redirect(http.StatusFound, ProviderURI+"/protocol/openid-connect/logout?post_logout_redirect_uri="+auth.serverURL+"/&client_id="+auth.clientID+"")
137+
}
138+
134139
// Middleware functions
135140

136141
func (auth *Auth) CookieMiddleware() gin.HandlerFunc {
137142
return func(c *gin.Context) {
138143
cookie, err := c.Cookie(CookieName)
139144
if err != nil {
140145
log.Error(CookieName, "cookie not found")
141-
c.Redirect(http.StatusFound, auth.authenticateURL+"?referer="+c.Request.URL.String())
146+
c.Redirect(http.StatusFound, auth.loginURL+"?referer="+c.Request.URL.String())
142147
return
143148
}
144149
err = auth.setGinContext(c, cookie)

0 commit comments

Comments
 (0)