-
Notifications
You must be signed in to change notification settings - Fork 53
/
middleware.go
55 lines (44 loc) · 1.08 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package auth
import (
"errors"
"github.com/gin-gonic/gin"
"google.golang.org/grpc"
)
const (
AuthorizationKey = "authorization"
)
type Protocol uint32
const (
ProtocolHTTP Protocol = 1 << iota
ProtocolUnaryGRPC
ProtocolStreamGRPC
)
type Middleware any
func SupportedProtocols(mw Middleware) Protocol {
var p Protocol
if _, ok := mw.(HTTPMiddleware); ok {
p |= ProtocolHTTP
}
if _, ok := mw.(UnaryGRPCMiddleware); ok {
p |= ProtocolUnaryGRPC
}
if _, ok := mw.(StreamGRPCMiddleware); ok {
p |= ProtocolStreamGRPC
}
return p
}
type HTTPMiddleware interface {
Handle(*gin.Context)
}
type UnaryGRPCMiddleware interface {
UnaryServerInterceptor() grpc.UnaryClientInterceptor
}
type StreamGRPCMiddleware interface {
StreamServerInterceptor() grpc.StreamServerInterceptor
}
var (
ErrInvalidMiddlewareName = errors.New("invalid or empty auth middleware name")
ErrMiddlewareAlreadyExists = errors.New("auth middleware already exists")
ErrNilMiddleware = errors.New("auth middleware is nil")
ErrMiddlewareNotFound = errors.New("auth middleware not found")
)