-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
55 lines (50 loc) · 1.98 KB
/
errors.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 secure
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// Status represents an error status.
type Status string
const (
// ErrInvalidToken is returned when the token is invalid.
ErrInvalidToken = Status("token is invalid")
// ErrExpiredToken is returned when the token is expired.
ErrExpiredToken = Status("token is expired")
// ErrInvalidTokenType is returned when the token type is invalid.
ErrInvalidTokenType = Status("token type is invalid")
// ErrInvalidTokenSignature is returned when the token signature is invalid.
ErrInvalidTokenSignature = Status("token signature is invalid")
// ErrUnsupportedSigningMethod is returned when the signing method is not supported.
ErrUnsupportedSigningMethod = Status("unsupported signing method")
// ErrUnsupportedTokenType is returned when the token type is not supported.
ErrUnsupportedTokenType = Status("unsupported token type")
// ErrUnsupportedOperation is returned when the operation is not supported.
ErrUnsupportedOperation = Status("unsupported operation")
// ErrInvalidAuthExprOutput is returned when the authorization expression does not return a boolean.
ErrInvalidAuthExprOutput = Status("authorization expression must return a boolean")
// ErrUnauthenticated is returned when the user is not authenticated.
ErrUnauthenticated = Status("unauthenticated")
// ErrPermissionDenied is returned when the user does not have permission to perform the operation.
ErrPermissionDenied = Status("permission denied")
)
func (s Status) Error() string {
return string(s)
}
// GRPCStatus returns the gRPC status for the error.
// Implements the GRPCStatus() method, see status.FromError(error).
func (s Status) GRPCStatus() *status.Status {
code := codes.Unknown
switch s {
case
ErrInvalidToken,
ErrExpiredToken,
ErrInvalidTokenType,
ErrInvalidTokenSignature,
ErrUnauthenticated:
code = codes.Unauthenticated
case
ErrPermissionDenied:
code = codes.PermissionDenied
}
return status.New(code, s.Error())
}