auth

package
v0.5.1-0...-3d89b33 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 15, 2020 License: MIT Imports: 3 Imported by: 0

README

blademaster/middleware/auth

项目简介

blademaster 的 authorization middleware,主要用于设置路由的认证策略

注:仅仅是个demo,请根据自身业务实现具体鉴权逻辑

Documentation

Overview

Example

This example create a identify middleware instance and attach to several path, it will validate request by specified policy and put extra information into context. e.g., `mid`. It provides additional handler functions to provide the identification for your business handler.

package main

import (
	"fmt"

	"github.com/go-kratos/kratos/example/blademaster/middleware/auth"
	bm "github.com/go-kratos/kratos/pkg/net/http/blademaster"
	"github.com/go-kratos/kratos/pkg/net/metadata"
)

func main() {
	myHandler := func(ctx *bm.Context) {
		mid := metadata.Int64(ctx, metadata.Mid)
		ctx.JSON(fmt.Sprintf("%d", mid), nil)
	}

	authn := auth.New(&auth.Config{
		DisableCSRF: false,
	})

	e := bm.DefaultServer(nil)

	// mark `/user` path as User policy
	e.GET("/user", authn.User, myHandler)
	// mark `/mobile` path as UserMobile policy
	e.GET("/mobile", authn.UserMobile, myHandler)
	// mark `/web` path as UserWeb policy
	e.GET("/web", authn.UserWeb, myHandler)
	// mark `/guest` path as Guest policy
	e.GET("/guest", authn.Guest, myHandler)

	o := e.Group("/owner", authn.User)
	o.GET("/info", myHandler)
	o.POST("/modify", myHandler)

	e.Start()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

Auth is the authorization middleware

func New

func New(conf *Config) *Auth

New is used to create an authorization middleware

func (*Auth) Guest

func (a *Auth) Guest(ctx *bm.Context)

Guest is used to mark path as guest policy. If `access_token` is exist in request form, it will using mobile access policy. Otherwise to web access policy.

func (*Auth) GuestMobile

func (a *Auth) GuestMobile(ctx *bm.Context)

GuestMobile is used to mark path as mobile guest policy.

func (*Auth) GuestWeb

func (a *Auth) GuestWeb(ctx *bm.Context)

GuestWeb is used to mark path as web guest policy.

func (*Auth) User

func (a *Auth) User(ctx *bm.Context)

User is used to mark path as access required. If `access_token` is exist in request form, it will using mobile access policy. Otherwise to web access policy.

func (*Auth) UserMobile

func (a *Auth) UserMobile(ctx *bm.Context)

UserMobile is used to mark path as mobile access required.

func (*Auth) UserWeb

func (a *Auth) UserWeb(ctx *bm.Context)

UserWeb is used to mark path as web access required.

type Config

type Config struct {
	// csrf switch.
	DisableCSRF bool
}

Config is the identify config model.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL