-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.go
50 lines (43 loc) · 1.24 KB
/
headers.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
package filebrowser
import (
"context"
"net/http"
"strconv"
"go.uber.org/zap"
"google.golang.org/grpc/metadata"
)
func intoInt32(s string) (int32, error) {
if raw, err := strconv.ParseInt(s, 10, 32); err != nil {
return 0, ErrInvalidHeader
} else {
return int32(raw), nil
}
}
func GetUidFromGrpcCtx(ctx context.Context, header string, logger *zap.Logger) (int32, error) {
if meta, exists := metadata.FromIncomingContext(ctx); !exists {
return 0, ErrUnauthorized
} else if values := meta.Get(header); len(values) == 0 {
return 0, ErrUnauthorized
} else if uid, err := intoInt32(values[0]); err != nil {
logger.Warn("parsing header data into int32",
zap.String("header", header),
zap.String("value", values[0]),
zap.Error(err))
return 0, ErrInvalidHeader
} else {
return uid, nil
}
}
func GetUidFromHttpRequest(r *http.Request, header string, logger *zap.Logger) (int32, error) {
if values, exits := r.Header[header]; !exits || len(values) == 0 {
return 0, ErrUnauthorized
} else if uid, err := intoInt32(values[0]); err != nil {
logger.Warn("parsing header data into int32",
zap.String("header", header),
zap.String("value", values[0]),
zap.Error(err))
return 0, ErrInvalidHeader
} else {
return uid, nil
}
}