Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show an example of gin middleware for authorization and authentication #551

Merged
merged 6 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ Note that at this time, we do not currently support rotating the master key.
- If does not exist...it uses a default config defined in the code inline
3. Finally, it loads the config/.env file and adds the env variables defined in this file to the final SSIServiceConfig

### Authentication and Authorization

The ssi server uses the Gin framework from Golang, which allows various kinds of middleware. Look in `pkg/middleware/Authentication.go` and `pkg/middleware/Authorization.go` for details on how you can wire up authentication and authorization for your use case.

## Pre-built images to use

There are pre-build images built by github actions on each merge to the main branch, which you can access here:
Expand Down
41 changes: 41 additions & 0 deletions pkg/server/middleware/Authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package middleware

import (
"net/http"

"github.com/gin-gonic/gin"
)

/*
To use this middleware, you need to add it to your gin router in server.go:

// setUpEngine creates the gin engine and sets up the middleware based on config
func setUpEngine(cfg config.ServerConfig, shutdown chan os.Signal) *gin.Engine {
gin.ForceConsoleColor()
middlewares := gin.HandlersChain{
gin.Recovery(),
gin.Logger(),
middleware.Errors(shutdown),
middleware.AuthMiddleware(),
middleware.AuthorizationMiddleware(),
}


*/

func AuthMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
// This is a dummy check here. You should do your actual JWT token verification.
if token == "IF YOU SET IT TO THIS VALUE IT WILL FAIL" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization is required"})
c.Abort()
return
}
// Assuming that the token is valid and we got the user info from the JWT token.
// You should replace it with actual user info.
user := "user"
c.Set("user", user)
c.Next()
}
}
45 changes: 45 additions & 0 deletions pkg/server/middleware/Authorization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package middleware

import (
"github.com/gin-gonic/gin"
)

/*
To use this middleware, you need to add it to your gin router in server.go:

// setUpEngine creates the gin engine and sets up the middleware based on config
func setUpEngine(cfg config.ServerConfig, shutdown chan os.Signal) *gin.Engine {
gin.ForceConsoleColor()
middlewares := gin.HandlersChain{
gin.Recovery(),
gin.Logger(),
middleware.Errors(shutdown),
middleware.AuthMiddleware(),
}

*/

func AuthorizationMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
userRole := "userRole" // Retrieve user role from the JWT or context
path := c.FullPath()

switch {

case userRole == "admin":
// Admin has access to every endpoint
c.Next()

case userRole == "user" && path != "/admin-endpoint":
// Users do not have access to the admin endpoint
c.Next()

default:
// Normally you would return a 403 here, but for the sake of the example we will just call next
// c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden"})
// c.Abort()
// return
c.Next()
}
}
}