func changeAccountPassword(ctx context.Context, w http.ResponseWriter, r *http.Request) { var acct account.Account var email string var newPassword string // read the account ID if emailBytes, err := base64.RawURLEncoding.DecodeString(rest.Param(ctx, "id")); err != nil { rest.WriteJSON(w, err) return } else { email = string(emailBytes) } if err := rest.ReadJSON(r, &newPassword); err != nil { rest.WriteJSON(w, err) } else if err := account.Get(ctx, email, &acct); err != nil { rest.WriteJSON(w, err) } else if err := acct.SetPassword(newPassword); err != nil { rest.WriteJSON(w, err) } else if err := account.Save(ctx, &acct); err != nil { rest.WriteJSON(w, err) } else { rest.WriteJSON(w, &rest.NoContent) } }
func changeAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) { var acct account.Account var resp rest.Response resp.Body = &acct var email string var err error // read the account ID if emailBytes, err := base64.RawURLEncoding.DecodeString(rest.Param(ctx, "id")); err != nil { rest.WriteJSON(w, err) return } else { email = string(emailBytes) } // read the account and merge it with the request body if err = account.Get(ctx, email, &acct); err != nil { rest.WriteJSON(w, err) return } else if err = rest.ReadJSON(r, &acct); err != nil { rest.WriteJSON(w, err) return } // is the email address changing? if email != acct.Email { // run the change, then reread the account if err = account.ChangeEmail(ctx, email, acct.Email); err != nil { rest.WriteJSON(w, err) return } email = acct.Email if err = account.Get(ctx, email, &acct); err != nil { rest.WriteJSON(w, err) return } // point to the new URL on response newAccountURL, _ := r.URL.Parse("..") newAccountURL, _ = newAccountURL.Parse(base64.RawURLEncoding.EncodeToString([]byte(acct.Email))) w.Header().Set("Location", newAccountURL.String()) resp.Code = http.StatusMovedPermanently } // save the remaining changes to the account if err = account.Save(ctx, &acct); err != nil { rest.WriteJSON(w, err) return } rest.WriteJSON(w, &resp) }
func getAccount(ctx context.Context, w http.ResponseWriter, r *http.Request) { accountId := rest.Param(ctx, "id") var acct account.Account if email, err := base64.RawURLEncoding.DecodeString(accountId); err != nil { rest.WriteJSON(w, err) } else if err := account.Get(ctx, string(email), &acct); err != nil { rest.WriteJSON(w, err) } else { rest.WriteJSON(w, &acct) } }
func getJwt(ctx context.Context, w http.ResponseWriter, r *http.Request) { var acct account.Account var email string var err error conf := config.Global{} if err := config.Get(ctx, &conf); err != nil { rest.WriteJSON(w, err) return } // read the account ID if emailBytes, err := base64.RawURLEncoding.DecodeString(rest.Param(ctx, "id")); err != nil { rest.WriteJSON(w, err) return } else { email = string(emailBytes) } if err = account.Get(ctx, email, &acct); err != nil { rest.WriteJSON(w, err) return } // All set. Generate the JWT. jwt, err := auth.Encode(&jws.ClaimSet{ Sub: acct.Email, Scope: strings.Join(acct.Roles, ","), Exp: time.Now().AddDate(1, 0, 0).Unix(), PrivateClaims: map[string]interface{}{ "n": "DEV USER", }, }, []byte(conf.AuthSecret)) if err != nil { rest.WriteJSON(w, errors.New(http.StatusInternalServerError, "Could not generate JWT")) return } else { jwtString := string(jwt) rest.WriteJSON(w, &jwtString) return } }