-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
50 lines (44 loc) · 1.16 KB
/
auth.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
49
50
package local_bucketing_proxy
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
func DevCycleAuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
sdkKey := ""
if sdkKeyParam, hasSDKKeyParam := c.GetQuery("sdkKey"); hasSDKKeyParam {
sdkKey = sdkKeyParam
}
if sdkKeyHeader := c.GetHeader("Authorization"); sdkKeyHeader != "" {
sdkKey = sdkKeyHeader
}
if sdkKey == "" {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": "Missing 'sdkKey' query parameter or 'Authorization' header",
"statusCode": http.StatusUnauthorized,
})
c.Next()
return
}
sdkKey = strings.ReplaceAll(sdkKey, "Bearer ", "")
sdkKeyType := ""
if strings.HasPrefix(sdkKey, "dvc") {
sdkKeyType = strings.Split(sdkKey, "_")[1]
} else {
sdkKeyType = strings.Split(sdkKey, "-")[0]
}
switch sdkKeyType {
case "server":
break
default:
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
"message": fmt.Sprintf("Only 'server', 'dvc_server' keys are supported by this API. Invalid key: %s", sdkKey),
"statusCode": http.StatusUnauthorized,
})
}
c.Set("dvc_sdk_key", sdkKey)
c.Next()
}
}