basic_auth

package module
v0.0.0-...-1305709 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 12 Imported by: 0

README

basic-auth

basic auth middleware for go-kratos

usage using go-kratos examples helloworld

package main

import (
	"context"
	"fmt"
	"log"

	baseAuth "github.com/czyt/basic-auth"
	"github.com/go-kratos/examples/helloworld/helloworld"
	"github.com/go-kratos/kratos/v2"
	"github.com/go-kratos/kratos/v2/errors"
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/go-kratos/kratos/v2/transport/http"
)

type server struct {
	helloworld.UnimplementedGreeterServer

	hc helloworld.GreeterClient
}

func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
	return &helloworld.HelloReply{Message: fmt.Sprintf("hello,%s", in.Name)}, nil
}

func main() {
	httpSrv := http.NewServer(
		http.Address(":8000"),
		http.Middleware(
			baseAuth.Server(baseAuth.WithAuthentication("czyt", "admin")),
		),
	)
	grpcSrv := grpc.NewServer(
		grpc.Address(":9000"),
		grpc.Middleware(
			baseAuth.Server(baseAuth.WithValidator(func(user string, pwd string, ctx context.Context) (bool, error) {
				if user == "czyt" && pwd == "grpc" {
					return true, nil
				}
				return false, errors.New(401, "授权失败", "账号或密码不正确!")
			})),
		),
	)
	con, _ := grpc.DialInsecure(
		context.Background(),
		grpc.WithEndpoint("dns:///127.0.0.1:9001"),
		grpc.WithMiddleware(
			baseAuth.Client(baseAuth.WithAuthentication("czyt", "admin")),
		),
	)
	s := &server{
		hc: helloworld.NewGreeterClient(con),
	}
	helloworld.RegisterGreeterServer(grpcSrv, s)
	helloworld.RegisterGreeterHTTPServer(httpSrv, s)
	app := kratos.New(
		kratos.Name("helloworld"),
		kratos.Server(
			httpSrv,
			grpcSrv,
		),
	)
	if err := app.Run(); err != nil {
		log.Fatal(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized    = errors.Unauthorized(reason, "Not authorized")
	ErrValidatorNotSet = errors.Unauthorized(reason, "basic auth validator is not set")
)

Functions

func Client

func Client(opts ...Option) middleware.Middleware

func Server

func Server(opts ...Option) middleware.Middleware

Types

type Option

type Option func(config *options)

func WithAuthentication

func WithAuthentication(userName, password string) Option

WithAuthentication use user specified userName and password for checking

func WithRealm

func WithRealm(realm string) Option

WithRealm sets the realm attribute of BasicAuth. the realm identifies the system to authenticate against and can be used by clients to save credentials Optional. Default: "Restricted". see https://datatracker.ietf.org/doc/html/rfc2617#section-2

func WithValidator

func WithValidator(validator Validator) Option

WithValidator allow user to impl username and passwordCheck

type Validator

type Validator func(string, string, context.Context) (bool, error)

Jump to

Keyboard shortcuts

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