Back to godoc.org

Package auth

v0.3.3
Latest Go to latest

The latest major version is .

Published: Jan 20, 2020 | License: MIT | Module: github.com/bilibili/kratos

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.

Code:

package main

import (
	"fmt"
	"github.com/bilibili/kratos/example/blademaster/middleware/auth"
	bm "github.com/bilibili/kratos/pkg/net/http/blademaster"
	"github.com/bilibili/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()
}

Index

Examples

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.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier