func (m *RethinkManager) CreateClient(c *fosite.DefaultClient) error { if c.ID == "" { c.ID = uuid.New() } hash, err := m.Hasher.Hash(c.Secret) if err != nil { return errors.New(err) } c.Secret = hash if err := m.publishCreate(c); err != nil { return err } return nil }
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { var c fosite.DefaultClient var ctx = herodot.NewContext() if err := json.NewDecoder(r.Body).Decode(&c); err != nil { h.H.WriteError(ctx, w, r, errors.New(err)) return } if _, err := h.W.HTTPActionAllowed(ctx, r, &ladon.Request{ Resource: ClientsResource, Action: "create", Context: ladon.Context{ "owner": c.Owner, }, }, Scope); err != nil { h.H.WriteError(ctx, w, r, err) return } if len(c.Secret) < 6 { secret, err := sequence.RuneSequence(12, []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_-.,:;$%!&/()=?+*#<>")) if err != nil { h.H.WriteError(ctx, w, r, errors.New(err)) return } c.Secret = []byte(string(secret)) } if err := h.Manager.CreateClient(&c); err != nil { h.H.WriteError(ctx, w, r, err) return } h.H.WriteCreated(ctx, w, r, ClientsHandlerPath+"/"+c.GetID(), &c) }