Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Latest commit

 

History

History
73 lines (59 loc) · 1.51 KB

README.md

File metadata and controls

73 lines (59 loc) · 1.51 KB

auth-middleware

auth-middleware is a package used to authenticate users from alif-service-user.

How to use

Include the module in your fx.Options when running the app:

import "github.com/alifcapital/auth-middleware"

...
modules := fx.Options(
	...
	alifcore_auth_middleware.Module,
	...
)

fx.New(modules).Run()
...

Pass the middleware on route endpoints:

import "github.com/alifcapital/auth-middleware/middleware"

// Params is the input parameter struct for the module that contains its dependencies
type Params struct {
    fx.In
    Mw     middleware.Middleware
    Srv    *gin.Engine
}

// NewPingHandler constructs a new ping.Handler.
func NewPingHandler(p Params) error {

    mw := p.Mw.Middleware
    p.Srv.GET("/ping", mw(Ping))
    p.Srv.Get("/ping-access", mw(Ping, "admin", "moder")) // Endpoint requiring admin and moder access

    return nil
}

func Ping(c *gin.Context) {
    c.JSON(200, gin.H{
        "message": "pong",
    })
}

Environment Variables

Place .env file in the base of your project

PUB_KEY_URI="http://alif-core-service-user.eu-central-1.elasticbeanstalk.com/service_user/auth/public_key"
PUB_KEY_DATA="{\"service_name\": \"alif-shop-settings\"}"
SERVICE_NAME="alif-shop-settings"

Checking info

3 values are passed from middleware:

id       string
username string
roles    map[string][]string

Get values passed from middleware in handlers:

func (t *handler) foobar(c *gin.Context) {
    if id, ok := c.Get("id"); ok {
        ...
    }
    ...