|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "encoding/json" |
| 8 | + "errors" |
| 9 | + "flag" |
| 10 | + "github.com/grepplabs/kafka-proxy/pkg/apis" |
| 11 | + "github.com/grepplabs/kafka-proxy/pkg/libs/util" |
| 12 | + "github.com/grepplabs/kafka-proxy/plugin/token-info/shared" |
| 13 | + "github.com/hashicorp/go-plugin" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | + "os" |
| 16 | + "strings" |
| 17 | + "time" |
| 18 | +) |
| 19 | + |
| 20 | +const ( |
| 21 | + StatusOK = 0 |
| 22 | + StatusEmptyToken = 1 |
| 23 | + StatusParseJWTFailed = 2 |
| 24 | + StatusWrongAlgorithm = 3 |
| 25 | + StatusUnauthorized = 4 |
| 26 | + StatusNoIssueTimeInToken = 5 |
| 27 | + StatusNoExpirationTimeInToken = 6 |
| 28 | + StatusTokenTooEarly = 7 |
| 29 | + StatusTokenExpired = 8 |
| 30 | + |
| 31 | + AlgorithmNone = "none" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + clockSkew = 1 * time.Minute |
| 36 | +) |
| 37 | + |
| 38 | +type UnsecuredJWTVerifier struct { |
| 39 | + claimSub map[string]struct{} |
| 40 | +} |
| 41 | + |
| 42 | +type pluginMeta struct { |
| 43 | + claimSub util.ArrayFlags |
| 44 | +} |
| 45 | + |
| 46 | +func (f *pluginMeta) flagSet() *flag.FlagSet { |
| 47 | + fs := flag.NewFlagSet("unsecured-jwt-info info settings", flag.ContinueOnError) |
| 48 | + return fs |
| 49 | +} |
| 50 | + |
| 51 | +// Implements apis.TokenInfo |
| 52 | +func (v UnsecuredJWTVerifier) VerifyToken(ctx context.Context, request apis.VerifyRequest) (apis.VerifyResponse, error) { |
| 53 | + if request.Token == "" { |
| 54 | + return getVerifyResponseResponse(StatusEmptyToken) |
| 55 | + } |
| 56 | + |
| 57 | + header, claimSet, err := Decode(request.Token) |
| 58 | + if err != nil { |
| 59 | + return getVerifyResponseResponse(StatusParseJWTFailed) |
| 60 | + } |
| 61 | + if header.Algorithm != AlgorithmNone { |
| 62 | + return getVerifyResponseResponse(StatusWrongAlgorithm) |
| 63 | + } |
| 64 | + |
| 65 | + if len(v.claimSub) != 0 { |
| 66 | + if _, ok := v.claimSub[claimSet.Sub]; !ok { |
| 67 | + return getVerifyResponseResponse(StatusUnauthorized) |
| 68 | + } |
| 69 | + } |
| 70 | + if claimSet.Iat < 1 { |
| 71 | + return getVerifyResponseResponse(StatusNoIssueTimeInToken) |
| 72 | + } |
| 73 | + if claimSet.Exp < 1 { |
| 74 | + return getVerifyResponseResponse(StatusNoExpirationTimeInToken) |
| 75 | + } |
| 76 | + |
| 77 | + earliest := int64(claimSet.Iat) - int64(clockSkew.Seconds()) |
| 78 | + latest := int64(claimSet.Exp) + int64(clockSkew.Seconds()) |
| 79 | + unix := time.Now().Unix() |
| 80 | + |
| 81 | + if unix < earliest { |
| 82 | + return getVerifyResponseResponse(StatusTokenTooEarly) |
| 83 | + } |
| 84 | + if unix > latest { |
| 85 | + return getVerifyResponseResponse(StatusTokenExpired) |
| 86 | + } |
| 87 | + return getVerifyResponseResponse(StatusOK) |
| 88 | +} |
| 89 | + |
| 90 | +type Header struct { |
| 91 | + Algorithm string `json:"alg"` |
| 92 | +} |
| 93 | + |
| 94 | +// kafka client sends float instead of int |
| 95 | +type ClaimSet struct { |
| 96 | + Sub string `json:"sub,omitempty"` |
| 97 | + Exp float64 `json:"exp"` |
| 98 | + Iat float64 `json:"iat"` |
| 99 | + OtherClaims map[string]interface{} `json:"-"` |
| 100 | +} |
| 101 | + |
| 102 | +func Decode(token string) (*Header, *ClaimSet, error) { |
| 103 | + args := strings.Split(token, ".") |
| 104 | + if len(args) < 2 { |
| 105 | + return nil, nil, errors.New("jws: invalid token received") |
| 106 | + } |
| 107 | + decodedHeader, err := base64.RawURLEncoding.DecodeString(args[0]) |
| 108 | + if err != nil { |
| 109 | + return nil, nil, err |
| 110 | + } |
| 111 | + decodedPayload, err := base64.RawURLEncoding.DecodeString(args[1]) |
| 112 | + if err != nil { |
| 113 | + return nil, nil, err |
| 114 | + } |
| 115 | + |
| 116 | + header := &Header{} |
| 117 | + err = json.NewDecoder(bytes.NewBuffer(decodedHeader)).Decode(header) |
| 118 | + if err != nil { |
| 119 | + return nil, nil, err |
| 120 | + } |
| 121 | + claimSet := &ClaimSet{} |
| 122 | + err = json.NewDecoder(bytes.NewBuffer(decodedPayload)).Decode(claimSet) |
| 123 | + if err != nil { |
| 124 | + return nil, nil, err |
| 125 | + } |
| 126 | + return header, claimSet, nil |
| 127 | +} |
| 128 | + |
| 129 | +func getVerifyResponseResponse(status int) (apis.VerifyResponse, error) { |
| 130 | + success := status == StatusOK |
| 131 | + return apis.VerifyResponse{Success: success, Status: int32(status)}, nil |
| 132 | +} |
| 133 | + |
| 134 | +func main() { |
| 135 | + pluginMeta := &pluginMeta{} |
| 136 | + fs := pluginMeta.flagSet() |
| 137 | + fs.Var(&pluginMeta.claimSub, "claim-sub", "Allowed subject claim (user name)") |
| 138 | + fs.Parse(os.Args[1:]) |
| 139 | + |
| 140 | + logrus.Infof("Unsecured JWT sub claims: %v", pluginMeta.claimSub) |
| 141 | + |
| 142 | + unsecuredJWTVerifier := &UnsecuredJWTVerifier{ |
| 143 | + claimSub: pluginMeta.claimSub.AsMap(), |
| 144 | + } |
| 145 | + |
| 146 | + plugin.Serve(&plugin.ServeConfig{ |
| 147 | + HandshakeConfig: shared.Handshake, |
| 148 | + Plugins: map[string]plugin.Plugin{ |
| 149 | + "unsecuredJWTInfo": &shared.TokenInfoPlugin{Impl: unsecuredJWTVerifier}, |
| 150 | + }, |
| 151 | + // A non-nil value here enables gRPC serving for this plugin... |
| 152 | + GRPCServer: plugin.DefaultGRPCServer, |
| 153 | + }) |
| 154 | +} |
0 commit comments