mattjackson220 commented on a change in pull request #6524:
URL: https://github.com/apache/trafficcontrol/pull/6524#discussion_r803894386



##########
File path: traffic_ops/traffic_ops_golang/cdni/shared.go
##########
@@ -0,0 +1,199 @@
+package cdni
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+       "errors"
+       "net/http"
+       "time"
+
+       "github.com/dgrijalva/jwt-go"
+
+       "github.com/apache/trafficcontrol/traffic_ops/traffic_ops_golang/api"
+)
+
+const CapabilityQuery = `SELECT id, type, ucdn FROM cdni_capabilities WHERE 
type = $1 AND ucdn = $2`
+const FootprintQuery = `SELECT footprint_type, footprint_value::text[] FROM 
cdni_footprints WHERE capability_id = $1`
+
+func GetCapabilities(w http.ResponseWriter, r *http.Request) {
+       inf, userErr, sysErr, errCode := api.NewInfo(r, nil, nil)
+       if userErr != nil || sysErr != nil {
+               api.HandleErr(w, r, inf.Tx.Tx, errCode, userErr, sysErr)
+               return
+       }
+       defer inf.Close()
+
+       bearerToken := r.Header.Get("Authorization")
+
+       if inf.Config.Cdni == nil || inf.Config.Cdni.JwtDecodingSecret == "" || 
inf.Config.Cdni.DCdnId == "" {
+               api.HandleErr(w, r, inf.Tx.Tx, http.StatusInternalServerError, 
nil, errors.New("cdn.conf does not contain CDNi information"))
+               return
+       }
+
+       claims := jwt.MapClaims{}
+       token, err := jwt.ParseWithClaims(bearerToken, claims, func(token 
*jwt.Token) (interface{}, error) {
+               return []byte(inf.Config.Cdni.JwtDecodingSecret), nil
+       })
+       if err != nil {
+               api.HandleErr(w, r, nil, http.StatusInternalServerError, 
errors.New("parsing claims"), nil)
+               return
+       }
+       if !token.Valid {
+               api.HandleErr(w, r, nil, http.StatusInternalServerError, 
errors.New("invalid token"), nil)
+               return
+       }
+
+       var expirationFloat float64
+       var ucdn string
+       var dcdn string
+       for key, val := range claims {
+               switch key {
+               case "iss":
+                       ucdn = val.(string)
+               case "aud":
+                       dcdn = val.(string)
+               case "exp":
+                       expirationFloat = val.(float64)

Review comment:
       👍 
   
   yea those are defined in the JWT RFC and their specific values are defined 
in the SVA spec for the capacity and configuration APIs. I'll add documentation 
for those so its more clear




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to