package raxmiddleware import ( "net/http" "os" "github.com/gin-gonic/gin" "github.rackspace.com/gorax/tools/passkey" "github.rackspace.com/gorax/tools/util" ) // CtxPassKey defines the key to store in the gin.Context const CtxPassKey = "passkey" // PassKey function will initiate a PassKey client and embed it into the gin context func PassKey(key *passkey.Client) gin.HandlerFunc { return func(c *gin.Context) { var ( passKeyClient *passkey.Client err error ) switch { case key == nil: passKeyClient, err = passkey.NewFromFile("netsec01", "", util.IsStaging(os.Getenv("ENVIRONMENT"))) if err != nil { AddError(c, http.StatusUnauthorized, err.Error()) return } case key != nil: passKeyClient = key default: AddError(c, http.StatusUnauthorized, "unable to initialize passkey") return } c.Set(CtxPassKey, passKeyClient) } }