package raxmiddleware import ( "net/http" "github.com/gin-gonic/gin" "github.rackspace.com/gorax/clients/dcxAutohost" raxidentity "github.rackspace.com/gorax/clients/raxIdentity" ) // CtxDCX defines the key to store in the gin.Context const CtxDCX = "dcx" // DCX : Initiates DCX client and embeds it into the gin context func DCX(username, password string, staging bool) gin.HandlerFunc { return func(c *gin.Context) { var token string switch { case username != "" && password != "": raxID, err := raxidentity.InternalAuth(username, password, staging) if err != nil { AddError(c, http.StatusUnauthorized, err.Error()) return } token = raxID.APIKey() case c.GetHeader("X-Auth-Token") != "": token = c.GetHeader("X-Auth-Token") default: AddError(c, http.StatusUnauthorized, "DCX/X-Auth-Token header is missing") return } dcxClient, err := dcxAutohost.NewWithToken(token, staging) if err != nil { AddError(c, http.StatusUnauthorized, err.Error()) return } c.Set(CtxDCX, dcxClient) } }