Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func main() {

// Tokens
auth.Get("/tokens", routes.TokensGet)
auth.Post("/tokens", routes.TokensCreate)
mux.Post("/tokens", routes.TokensCreate)
auth.Delete("/tokens", routes.TokensDelete)

// Threads
Expand Down
2 changes: 1 addition & 1 deletion models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Token struct {
Expiring
Resource

// Type describes the token's purpose: auth, invite, upgrade, etc.
// Type describes the token's purpose: auth, invite, confirm, upgrade.
Type string `json:"type" gorethink:"type"`
}

Expand Down
4 changes: 2 additions & 2 deletions routes/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type AccountsCreateResponse struct {
func AccountsCreate(w http.ResponseWriter, r *http.Request) {
// Decode the request
var input AccountsCreateRequest
err := utils.ParseRequest(r, input)
err := utils.ParseRequest(r, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
Expand All @@ -56,7 +56,7 @@ func AccountsCreate(w http.ResponseWriter, r *http.Request) {
}

// Ensure that the user with requested username doesn't exist
if _, err := env.Accounts.FindAccountByName(input.Username); err != nil {
if _, err := env.Accounts.FindAccountByName(input.Username); err == nil {
utils.JSONResponse(w, 409, &AccountsCreateResponse{
Success: false,
Message: "Username already exists",
Expand Down
13 changes: 12 additions & 1 deletion routes/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TokensGet(c web.C, w http.ResponseWriter, r *http.Request) {
type TokensCreateRequest struct {
Username string `json:"username" schema:"username"`
Password string `json:"password" schema:"password"`
Type string `json:"type" schema:"type"`
}

// TokensCreateResponse contains the result of the TokensCreate request.
Expand All @@ -50,7 +51,7 @@ type TokensCreateResponse struct {
func TokensCreate(w http.ResponseWriter, r *http.Request) {
// Decode the request
var input TokensCreateRequest
err := utils.ParseRequest(r, input)
err := utils.ParseRequest(r, &input)
if err != nil {
env.Log.WithFields(logrus.Fields{
"error": err,
Expand All @@ -63,6 +64,15 @@ func TokensCreate(w http.ResponseWriter, r *http.Request) {
return
}

// We can only create "auth" tokens now
if input.Type != "auth" {
utils.JSONResponse(w, 409, &TokensCreateResponse{
Success: false,
Message: "Only auth tokens are implemented",
})
return
}

// Check if account exists
user, err := env.Accounts.FindAccountByName(input.Username)
if err != nil {
Expand Down Expand Up @@ -101,6 +111,7 @@ func TokensCreate(w http.ResponseWriter, r *http.Request) {
token := &models.Token{
Expiring: models.Expiring{expDate},
Resource: models.MakeResource(user.ID, "Auth token expiring on "+expDate.Format(time.RFC3339)),
Type: input.Type,
}

// Insert int into the database
Expand Down
3 changes: 3 additions & 0 deletions utils/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func JSONResponse(w http.ResponseWriter, status int, data interface{}) {
result = []byte(`{"status":500,"message":"Error occured while marshalling the response body"}`)
}

// Set the response's content type to JSON
w.Header().Set("Content-Type", "application/json; charset=utf-8")

// Write the result
w.WriteHeader(status)
w.Write(result)
Expand Down