|
| 1 | +/* |
| 2 | + * Copyright 2020 Dgraph Labs, Inc. and Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package authorization |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "context" |
| 22 | + "crypto/rsa" |
| 23 | + "encoding/json" |
| 24 | + "fmt" |
| 25 | + "net/http" |
| 26 | + "regexp" |
| 27 | + "strings" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/dgrijalva/jwt-go" |
| 31 | + "github.com/pkg/errors" |
| 32 | + "google.golang.org/grpc/metadata" |
| 33 | +) |
| 34 | + |
| 35 | +type ctxKey string |
| 36 | + |
| 37 | +const ( |
| 38 | + AuthJwtCtxKey = ctxKey("authorizationJwt") |
| 39 | + RSA256 = "RS256" |
| 40 | + HMAC256 = "HS256" |
| 41 | +) |
| 42 | + |
| 43 | +var ( |
| 44 | + metainfo = &AuthMeta{} |
| 45 | +) |
| 46 | + |
| 47 | +type AuthMeta struct { |
| 48 | + HMACPublicKey string |
| 49 | + RSAPublicKey *rsa.PublicKey |
| 50 | + Header string |
| 51 | + Namespace string |
| 52 | + Algo string |
| 53 | +} |
| 54 | + |
| 55 | +func (m *AuthMeta) Parse(schema string) error { |
| 56 | + lastCommentIdx := strings.LastIndex(schema, "#") |
| 57 | + if lastCommentIdx == -1 { |
| 58 | + return nil |
| 59 | + } |
| 60 | + lastComment := schema[lastCommentIdx:] |
| 61 | + if !strings.HasPrefix(lastComment, "# Authorization") { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + // This regex matches authorization information present in the last line of the schema. |
| 66 | + // Format: # Authorization <HTTP header> <Claim namespace> <Algorithm> "<verification key>" |
| 67 | + // Example: # Authorization X-Test-Auth https://xyz.io/jwt/claims HS256 "secretkey" |
| 68 | + // On successful regex match the index for the following strings will be returned. |
| 69 | + // [0][0]:[0][1] : # Authorization X-Test-Auth https://xyz.io/jwt/claims HS256 "secretkey" |
| 70 | + // [0][2]:[0][3] : Authorization, [0][4]:[0][5] : X-Test-Auth, |
| 71 | + // [0][6]:[0][7] : https://xyz.io/jwt/claims, |
| 72 | + // [0][8]:[0][9] : HS256, [0][10]:[0][11] : secretkey |
| 73 | + authMetaRegex, err := |
| 74 | + regexp.Compile(`^#[\s]([^\s]+)[\s]+([^\s]+)[\s]+([^\s]+)[\s]+([^\s]+)[\s]+"([^\"]+)"`) |
| 75 | + if err != nil { |
| 76 | + return errors.Errorf("error while parsing jwt authorization info: %v", err) |
| 77 | + } |
| 78 | + idx := authMetaRegex.FindAllStringSubmatchIndex(lastComment, -1) |
| 79 | + if len(idx) != 1 || len(idx[0]) != 12 || |
| 80 | + !strings.HasPrefix(lastComment, lastComment[idx[0][0]:idx[0][1]]) { |
| 81 | + return errors.Errorf("error while parsing jwt authorization info") |
| 82 | + } |
| 83 | + |
| 84 | + m.Header = lastComment[idx[0][4]:idx[0][5]] |
| 85 | + m.Namespace = lastComment[idx[0][6]:idx[0][7]] |
| 86 | + m.Algo = lastComment[idx[0][8]:idx[0][9]] |
| 87 | + |
| 88 | + key := lastComment[idx[0][10]:idx[0][11]] |
| 89 | + if m.Algo == HMAC256 { |
| 90 | + m.HMACPublicKey = key |
| 91 | + return nil |
| 92 | + } |
| 93 | + if m.Algo != RSA256 { |
| 94 | + return errors.Errorf( |
| 95 | + "invalid jwt algorithm: found %s, but supported options are HS256 or RS256", m.Algo) |
| 96 | + } |
| 97 | + |
| 98 | + // The jwt library internally uses `bytes.IndexByte(data, '\n')` to fetch new line and fails |
| 99 | + // if we have newline "\n" as ASCII value {92,110} instead of the actual ASCII value of 10. |
| 100 | + // To fix this we replace "\n" with new line's ASCII value. |
| 101 | + bytekey := bytes.ReplaceAll([]byte(key), []byte{92, 110}, []byte{10}) |
| 102 | + |
| 103 | + m.RSAPublicKey, err = jwt.ParseRSAPublicKeyFromPEM(bytekey) |
| 104 | + return err |
| 105 | +} |
| 106 | + |
| 107 | +func ParseAuthMeta(schema string) error { |
| 108 | + return metainfo.Parse(schema) |
| 109 | +} |
| 110 | + |
| 111 | +// AttachAuthorizationJwt adds any incoming JWT authorization data into the grpc context metadata. |
| 112 | +func AttachAuthorizationJwt(ctx context.Context, r *http.Request) context.Context { |
| 113 | + authorizationJwt := r.Header.Get(metainfo.Header) |
| 114 | + if authorizationJwt == "" { |
| 115 | + return ctx |
| 116 | + } |
| 117 | + |
| 118 | + md, ok := metadata.FromIncomingContext(ctx) |
| 119 | + if !ok { |
| 120 | + md = metadata.New(nil) |
| 121 | + } |
| 122 | + |
| 123 | + md.Append(string(AuthJwtCtxKey), authorizationJwt) |
| 124 | + ctx = metadata.NewIncomingContext(ctx, md) |
| 125 | + return ctx |
| 126 | +} |
| 127 | + |
| 128 | +type CustomClaims struct { |
| 129 | + AuthVariables map[string]interface{} |
| 130 | + jwt.StandardClaims |
| 131 | +} |
| 132 | + |
| 133 | +func (c *CustomClaims) UnmarshalJSON(data []byte) error { |
| 134 | + // Unmarshal the standard claims first. |
| 135 | + if err := json.Unmarshal(data, &c.StandardClaims); err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + var result map[string]interface{} |
| 140 | + if err := json.Unmarshal(data, &result); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + |
| 144 | + // Unmarshal the auth variables for a particular namespace. |
| 145 | + if authVariables, ok := result[metainfo.Namespace]; ok { |
| 146 | + c.AuthVariables, _ = authVariables.(map[string]interface{}) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func ExtractAuthVariables(ctx context.Context) (map[string]interface{}, error) { |
| 152 | + // Extract the jwt and unmarshal the jwt to get the auth variables. |
| 153 | + md, ok := metadata.FromIncomingContext(ctx) |
| 154 | + if !ok { |
| 155 | + return nil, nil |
| 156 | + } |
| 157 | + |
| 158 | + jwtToken := md.Get(string(AuthJwtCtxKey)) |
| 159 | + if len(jwtToken) == 0 { |
| 160 | + return nil, nil |
| 161 | + } else if len(jwtToken) > 1 { |
| 162 | + return nil, fmt.Errorf("invalid jwt auth token") |
| 163 | + } |
| 164 | + |
| 165 | + return validateToken(jwtToken[0]) |
| 166 | +} |
| 167 | + |
| 168 | +func validateToken(jwtStr string) (map[string]interface{}, error) { |
| 169 | + if metainfo.Algo == "" { |
| 170 | + return nil, fmt.Errorf( |
| 171 | + "jwt token cannot be validated because verification algorithm is not set") |
| 172 | + } |
| 173 | + |
| 174 | + token, err := |
| 175 | + jwt.ParseWithClaims(jwtStr, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { |
| 176 | + algo, _ := token.Header["alg"].(string) |
| 177 | + if algo != metainfo.Algo { |
| 178 | + return nil, errors.Errorf("unexpected signing method: Expected %s Found %s", |
| 179 | + metainfo.Algo, algo) |
| 180 | + } |
| 181 | + if algo == HMAC256 { |
| 182 | + if _, ok := token.Method.(*jwt.SigningMethodHMAC); ok { |
| 183 | + return []byte(metainfo.HMACPublicKey), nil |
| 184 | + } |
| 185 | + } else if algo == RSA256 { |
| 186 | + if _, ok := token.Method.(*jwt.SigningMethodRSA); ok { |
| 187 | + return metainfo.RSAPublicKey, nil |
| 188 | + } |
| 189 | + } |
| 190 | + return nil, errors.Errorf("couldn't parse signing method from token header: %s", algo) |
| 191 | + }) |
| 192 | + |
| 193 | + if err != nil { |
| 194 | + return nil, errors.Errorf("unable to parse jwt token:%v", err) |
| 195 | + } |
| 196 | + |
| 197 | + claims, ok := token.Claims.(*CustomClaims) |
| 198 | + if !ok || !token.Valid { |
| 199 | + return nil, errors.Errorf("claims in jwt token is not map claims") |
| 200 | + } |
| 201 | + |
| 202 | + // by default, the MapClaims.Valid will return true if the exp field is not set |
| 203 | + // here we enforce the checking to make sure that the refresh token has not expired |
| 204 | + now := time.Now().Unix() |
| 205 | + if !claims.VerifyExpiresAt(now, true) { |
| 206 | + return nil, errors.Errorf("Token is expired") // the same error msg that's used inside jwt-go |
| 207 | + } |
| 208 | + |
| 209 | + return claims.AuthVariables, nil |
| 210 | +} |
0 commit comments