本文整理汇总了Golang中github.com/tsuru/tsuru/auth.Token类的典型用法代码示例。如果您正苦于以下问题:Golang Token类的具体用法?Golang Token怎么用?Golang Token使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Token类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setEnv
func setEnv(w http.ResponseWriter, r *http.Request, t *auth.Token) error {
msg := "You must provide the environment variables in a JSON object"
if r.Body == nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
var variables map[string]string
err := json.NewDecoder(r.Body).Decode(&variables)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
u, err := t.User()
if err != nil {
return err
}
appName := r.URL.Query().Get(":app")
rec.Log(u.Email, "set-env", "app="+appName, variables)
app, err := getApp(appName, u)
if err != nil {
return err
}
envs := make([]bind.EnvVar, 0, len(variables))
for k, v := range variables {
envs = append(envs, bind.EnvVar{Name: k, Value: v, Public: true})
}
return app.SetEnvs(envs, true)
}
示例2: serviceUpdate
func serviceUpdate(w http.ResponseWriter, r *http.Request, t auth.Token) error {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
var y serviceYaml
err = yaml.Unmarshal(body, &y)
if err != nil {
return err
}
err = y.validate()
if err != nil {
return err
}
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "update-service", y.Id, y.Endpoint)
s, err := getServiceByOwner(y.Id, u)
if err != nil {
return err
}
s.Endpoint = y.Endpoint
s.Password = y.Password
s.Username = y.Username
if err = s.Update(); err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
示例3: revokeServiceAccess
func revokeServiceAccess(w http.ResponseWriter, r *http.Request, t auth.Token) error {
u, err := t.User()
if err != nil {
return err
}
serviceName := r.URL.Query().Get(":service")
teamName := r.URL.Query().Get(":team")
rec.Log(u.Email, "revoke-service-access", "service="+serviceName, "team="+teamName)
service, team, err := getServiceAndTeam(serviceName, teamName, u)
if err != nil {
return err
}
if len(service.Teams) < 2 {
msg := "You can not revoke the access from this team, because it is the unique team with access to this service, and a service can not be orphaned"
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
err = service.RevokeAccess(team)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
return conn.Services().Update(bson.M{"_id": service.Name}, service)
}
示例4: removeUserFromTeam
func removeUserFromTeam(w http.ResponseWriter, r *http.Request, t auth.Token) error {
email := r.URL.Query().Get(":user")
teamName := r.URL.Query().Get(":team")
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "remove-user-from-team", "team="+teamName, "user="+email)
conn, err := db.Conn()
if err != nil {
return err
}
defer conn.Close()
team, err := auth.GetTeam(teamName)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: "Team not found"}
}
if !team.ContainsUser(u) {
msg := fmt.Sprintf("You are not authorized to remove a member from the team %s", team.Name)
return &errors.HTTP{Code: http.StatusUnauthorized, Message: msg}
}
if len(team.Users) == 1 {
msg := "You can not remove this user from this team, because it is the last user within the team, and a team can not be orphaned"
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
user, err := auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
err = removeUserFromTeamInDatabase(user, team)
if err != nil {
return err
}
return removeUserFromTeamInRepository(user, team)
}
示例5: setNodeStatus
// title: set node status
// path: /node/status
// method: POST
// consume: application/x-www-form-urlencoded
// produce: application/json
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
// 404: App or unit not found
func setNodeStatus(w http.ResponseWriter, r *http.Request, t auth.Token) error {
if t.GetAppName() != app.InternalAppName {
return &errors.HTTP{Code: http.StatusForbidden, Message: "this token is not allowed to execute this action"}
}
err := r.ParseForm()
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
var hostInput provision.NodeStatusData
dec := form.NewDecoder(nil)
dec.IgnoreUnknownKeys(true)
err = dec.DecodeValues(&hostInput, r.Form)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
result, err := app.UpdateNodeStatus(hostInput)
if err != nil {
if err == provision.ErrNodeNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
return err
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(result)
}
示例6: unsetCName
func unsetCName(w http.ResponseWriter, r *http.Request, t auth.Token) error {
msg := "You must provide the cname."
if r.Body == nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
var v map[string][]string
err := json.NewDecoder(r.Body).Decode(&v)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: "Invalid JSON in request body."}
}
if _, ok := v["cname"]; !ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
u, err := t.User()
if err != nil {
return err
}
appName := r.URL.Query().Get(":app")
rawCName := strings.Join(v["cname"], ", ")
rec.Log(u.Email, "remove-cname", "app="+appName, "cnames="+rawCName)
app, err := getAppFromContext(appName, u, r)
if err != nil {
return err
}
if err = app.RemoveCName(v["cname"]...); err == nil {
return nil
}
if err.Error() == "Invalid cname" {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
return err
}
示例7: changePassword
func changePassword(w http.ResponseWriter, r *http.Request, t auth.Token) error {
managed, ok := app.AuthScheme.(auth.ManagedScheme)
if !ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: nonManagedSchemeMsg}
}
var body map[string]string
err := json.NewDecoder(r.Body).Decode(&body)
if err != nil {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: "Invalid JSON.",
}
}
if body["old"] == "" || body["new"] == "" {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: "Both the old and the new passwords are required.",
}
}
err = managed.ChangePassword(t, body["old"], body["new"])
if err != nil {
return handleAuthError(err)
}
rec.Log(t.GetUserName(), "change-password")
return nil
}
示例8: addUnits
func addUnits(w http.ResponseWriter, r *http.Request, t auth.Token) error {
n, err := numberOfUnits(r)
if err != nil {
return err
}
processName := r.FormValue("process")
appName := r.URL.Query().Get(":app")
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "add-units", "app="+appName, fmt.Sprintf("units=%d", n))
app, err := getApp(appName, u, r)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/json")
writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(w)}
err = app.AddUnits(n, processName, writer)
if err != nil {
writer.Encode(tsuruIo.SimpleJsonMessage{Error: err.Error()})
return nil
}
return nil
}
示例9: setUnitsStatus
func setUnitsStatus(w http.ResponseWriter, r *http.Request, t auth.Token) error {
if t.GetAppName() != app.InternalAppName {
return &errors.HTTP{Code: http.StatusForbidden, Message: "this token is not allowed to execute this action"}
}
defer r.Body.Close()
var input []map[string]string
err := json.NewDecoder(r.Body).Decode(&input)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
units := make(map[string]provision.Status, len(input))
for _, unit := range input {
units[unit["ID"]] = provision.Status(unit["Status"])
}
result, err := app.UpdateUnitsStatus(units)
if err != nil {
return err
}
resp := make([]updateUnitsResponse, 0, len(result))
for unit, found := range result {
resp = append(resp, updateUnitsResponse{ID: unit, Found: found})
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
return json.NewEncoder(w).Encode(resp)
}
示例10: revokeServiceAccess
func revokeServiceAccess(w http.ResponseWriter, r *http.Request, t auth.Token) error {
serviceName := r.URL.Query().Get(":service")
s, err := getService(serviceName)
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermServiceUpdateRevokeAccess,
append(permission.Contexts(permission.CtxTeam, s.OwnerTeams),
permission.Context(permission.CtxService, s.Name),
)...,
)
if !allowed {
return permission.ErrUnauthorized
}
teamName := r.URL.Query().Get(":team")
team, err := auth.GetTeam(teamName)
if err != nil {
if err == auth.ErrTeamNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: "Team not found"}
}
return err
}
if len(s.Teams) < 2 {
msg := "You can not revoke the access from this team, because it is the unique team with access to this service, and a service can not be orphaned"
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
rec.Log(t.GetUserName(), "revoke-service-access", "service="+serviceName, "team="+teamName)
err = s.RevokeAccess(team)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
return s.Update()
}
示例11: createServiceInstance
func createServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
var body map[string]string
err = json.Unmarshal(b, &body)
if err != nil {
return err
}
serviceName := body["service_name"]
user, err := t.User()
if err != nil {
return err
}
rec.Log(user.Email, "create-service-instance", string(b))
srv, err := getServiceOrError(serviceName, user)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
instance := service.ServiceInstance{
Name: body["name"],
PlanName: body["plan"],
TeamOwner: body["owner"],
}
return service.CreateServiceInstance(instance, &srv, user)
}
示例12: grantServiceAccess
func grantServiceAccess(w http.ResponseWriter, r *http.Request, t auth.Token) error {
serviceName := r.URL.Query().Get(":service")
s, err := getService(serviceName)
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermServiceUpdateGrantAccess,
append(permission.Contexts(permission.CtxTeam, s.OwnerTeams),
permission.Context(permission.CtxService, s.Name),
)...,
)
if !allowed {
return permission.ErrUnauthorized
}
teamName := r.URL.Query().Get(":team")
team, err := auth.GetTeam(teamName)
if err != nil {
if err == auth.ErrTeamNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: "Team not found"}
}
return err
}
rec.Log(t.GetUserName(), "grant-service-access", "service="+serviceName, "team="+teamName)
err = s.GrantAccess(team)
if err != nil {
return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()}
}
return s.Update()
}
示例13: serviceDelete
func serviceDelete(w http.ResponseWriter, r *http.Request, t auth.Token) error {
s, err := getService(r.URL.Query().Get(":name"))
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermServiceDelete,
append(permission.Contexts(permission.CtxTeam, s.OwnerTeams),
permission.Context(permission.CtxService, s.Name),
)...,
)
if !allowed {
return permission.ErrUnauthorized
}
rec.Log(t.GetUserName(), "delete-service", r.URL.Query().Get(":name"))
instances, err := service.GetServiceInstancesByServices([]service.Service{s})
if err != nil {
return err
}
if len(instances) > 0 {
msg := "This service cannot be removed because it has instances.\nPlease remove these instances before removing the service."
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
err = s.Delete()
if err != nil {
return err
}
w.WriteHeader(http.StatusNoContent)
return nil
}
示例14: unsetEnv
func unsetEnv(w http.ResponseWriter, r *http.Request, t *auth.Token) error {
msg := "You must provide the list of environment variables, in JSON format"
if r.Body == nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
var variables []string
defer r.Body.Close()
err := json.NewDecoder(r.Body).Decode(&variables)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
if len(variables) == 0 {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
appName := r.URL.Query().Get(":app")
u, err := t.User()
if err != nil {
return err
}
rec.Log(u.Email, "unset-env", "app="+appName, fmt.Sprintf("envs=%s", variables))
app, err := getApp(appName, u)
if err != nil {
return err
}
return app.UnsetEnvs(variables, true)
}
示例15: runCommand
func runCommand(w http.ResponseWriter, r *http.Request, t auth.Token) error {
w.Header().Set("Content-Type", "text")
msg := "You must provide the command to run"
if r.Body == nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
c, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
if len(c) < 1 {
return &errors.HTTP{Code: http.StatusBadRequest, Message: msg}
}
u, err := t.User()
if err != nil {
return err
}
appName := r.URL.Query().Get(":app")
once := r.URL.Query().Get("once")
rec.Log(u.Email, "run-command", "app="+appName, "command="+string(c))
app, err := getAppFromContext(appName, u, r)
if err != nil {
return err
}
keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 30*time.Second, "")
defer keepAliveWriter.Stop()
writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
err = app.Run(string(c), writer, once == "true")
if err != nil {
writer.Encode(tsuruIo.SimpleJsonMessage{Error: err.Error()})
return err
}
return nil
}