-
Notifications
You must be signed in to change notification settings - Fork 16
/
query_validate_jwt.go
48 lines (38 loc) · 1.16 KB
/
query_validate_jwt.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
package keeper
import (
"context"
"time"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/burnt-labs/xion/x/jwk/types"
)
func (k Keeper) ValidateJWT(goCtx context.Context, req *types.QueryValidateJWTRequest) (*types.QueryValidateJWTResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)
audience, exists := k.GetAudience(ctx, req.Aud)
if !exists {
return nil, status.Error(codes.NotFound, "not found")
}
key, err := jwk.ParseKey([]byte(audience.Key))
if err != nil {
return nil, err
}
if _, err := jwt.Parse([]byte(req.SigBytes),
jwt.WithKey(key.Algorithm(), key),
jwt.WithAudience(req.Aud),
jwt.WithSubject(req.Sub),
jwt.WithClock(jwt.ClockFunc(func() time.Time {
// adjust the time from the block-height due to lagging reported time
return ctx.BlockTime().Add(time.Duration(k.GetTimeOffset(ctx)))
})),
jwt.WithValidate(true),
); err != nil {
return nil, err
}
return &types.QueryValidateJWTResponse{}, nil
}