|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/golang-jwt/jwt/v5" |
| 9 | + "github.com/spf13/cobra" |
| 10 | + |
| 11 | + sdkclient "github.com/cosmos/cosmos-sdk/client" |
| 12 | + |
| 13 | + ajwt "github.com/akash-network/akash-api/go/util/jwt" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + FlagJWTExp = "exp" |
| 18 | + FlagJWTNbf = "nbf" |
| 19 | + FlagJWTAccess = "access" |
| 20 | + FlagJWTScope = "scope" |
| 21 | +) |
| 22 | + |
| 23 | +func AuthCmd() *cobra.Command { |
| 24 | + cmd := &cobra.Command{ |
| 25 | + Use: "auth", |
| 26 | + } |
| 27 | + |
| 28 | + cmd.AddCommand(authJWTCmd()) |
| 29 | + |
| 30 | + return cmd |
| 31 | +} |
| 32 | + |
| 33 | +func authJWTCmd() *cobra.Command { |
| 34 | + cmd := &cobra.Command{ |
| 35 | + Use: "jwt", |
| 36 | + Args: cobra.NoArgs, |
| 37 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 38 | + cctx, err := sdkclient.GetClientTxContext(cmd) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + signer := ajwt.NewSigner(cctx.Keyring, cctx.FromAddress) |
| 44 | + |
| 45 | + now := time.Now() |
| 46 | + nbf := now |
| 47 | + |
| 48 | + expString, err := cmd.Flags().GetString(FlagJWTExp) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + var exp time.Time |
| 54 | + // first, attempt to parse expiration value as duration. |
| 55 | + // fallback to unix timestamp if fails |
| 56 | + dur, err := time.ParseDuration(expString) |
| 57 | + if err != nil { |
| 58 | + expInt, err := strconv.ParseInt(expString, 10, 64) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + exp = time.Unix(expInt, 0) |
| 64 | + } else { |
| 65 | + exp = now.Add(dur) |
| 66 | + } |
| 67 | + |
| 68 | + if cmd.Flags().Changed(FlagJWTNbf) { |
| 69 | + nbfString, err := cmd.Flags().GetString(FlagJWTNbf) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + // first, attempt to parse expiration value as duration. |
| 75 | + // fallback to unix timestamp if fails |
| 76 | + dur, err := time.ParseDuration(nbfString) |
| 77 | + if err != nil { |
| 78 | + nbfInt, err := strconv.ParseInt(nbfString, 10, 64) |
| 79 | + if err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + |
| 83 | + exp = time.Unix(nbfInt, 0) |
| 84 | + } else { |
| 85 | + exp = now.Add(dur) |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + accessString, err := cmd.Flags().GetString(FlagJWTAccess) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + access, valid := parseAccess(accessString) |
| 95 | + if !valid { |
| 96 | + return fmt.Errorf("invalid `access` flags") |
| 97 | + } |
| 98 | + |
| 99 | + var scope ajwt.PermissionScopes |
| 100 | + |
| 101 | + if cmd.Flags().Changed(FlagJWTScope) { |
| 102 | + scopeString, err := cmd.Flags().GetString(FlagJWTAccess) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if err = scope.UnmarshalCSV(scopeString); err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if !exp.After(now) { |
| 113 | + return fmt.Errorf("`exp` value is invalid or in the past. expected %d (exp) > %d (curr)", exp.Unix(), now.Unix()) |
| 114 | + } |
| 115 | + |
| 116 | + if !nbf.After(exp) { |
| 117 | + return fmt.Errorf("`nbf` value is invalid. expected %d (nbf) < %d (exp)", nbf.Unix(), exp.Unix()) |
| 118 | + } |
| 119 | + |
| 120 | + claims := ajwt.Claims{ |
| 121 | + RegisteredClaims: jwt.RegisteredClaims{ |
| 122 | + Issuer: cctx.FromAddress.String(), |
| 123 | + IssuedAt: jwt.NewNumericDate(now), |
| 124 | + NotBefore: jwt.NewNumericDate(nbf), |
| 125 | + ExpiresAt: jwt.NewNumericDate(exp), |
| 126 | + }, |
| 127 | + Version: "v1", |
| 128 | + Leases: ajwt.Leases{ |
| 129 | + Access: access, |
| 130 | + Scope: scope, |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + tok := jwt.NewWithClaims(ajwt.SigningMethodES256K, &claims) |
| 135 | + |
| 136 | + tokString, err := tok.SignedString(signer) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + |
| 141 | + return cctx.PrintString(tokString) |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + cmd.Flags().String(FlagJWTExp, "15m", "Set token's `exp` field. Format is either 15m|h or unix timestamp") |
| 146 | + cmd.Flags().String(FlagJWTNbf, "", "Set token's `nbf` field. Format is either 15m|h or unix timestamp. Empty equals to current timestamp") |
| 147 | + cmd.Flags().String(FlagJWTAccess, "full", "Set token's `leases.access` field. Permitted values are full|scoped|granular. Default is full") |
| 148 | + cmd.Flags().StringSlice(FlagJWTScope, nil, "Set token's `leases.scope` field. Comma separated list of scopes. Can only be set if `leases.access=scoped`. Allowed scopes are") |
| 149 | + |
| 150 | + return cmd |
| 151 | +} |
| 152 | + |
| 153 | +func parseAccess(val string) (ajwt.AccessType, bool) { |
| 154 | + res := ajwt.AccessType(val) |
| 155 | + |
| 156 | + switch res { |
| 157 | + case ajwt.AccessTypeFull: |
| 158 | + case ajwt.AccessTypeScoped: |
| 159 | + case ajwt.AccessTypeGranular: |
| 160 | + default: |
| 161 | + return ajwt.AccessTypeNone, false |
| 162 | + } |
| 163 | + |
| 164 | + return res, true |
| 165 | +} |
0 commit comments