Initial commit
parent
a1fcb82f7c
commit
549cce3756
|
@ -0,0 +1,12 @@
|
||||||
|
# Binaries for programs and plugins
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
|
*.out
|
26
README.md
26
README.md
|
@ -1,3 +1,27 @@
|
||||||
# backend-login
|
# backend-login
|
||||||
|
|
||||||
Beep backend handling login.
|
Beep backend handling login. For now, just a POST endpoint returning a JWT. In the furture, SMS-based perpetual login.
|
||||||
|
|
||||||
|
## API (temporary)
|
||||||
|
|
||||||
|
```
|
||||||
|
POST /login
|
||||||
|
```
|
||||||
|
|
||||||
|
### Body
|
||||||
|
|
||||||
|
| Name | Type | Description | Required |
|
||||||
|
| ---- | ---- | ----------- | -------- |
|
||||||
|
| user | String | User's ID. | ✓ |
|
||||||
|
| device | String | Device's ID. Must be unique to the device. I suggest something based on MAC address. | ✓ |
|
||||||
|
|
||||||
|
### Success (200 OK)
|
||||||
|
|
||||||
|
JWT token.
|
||||||
|
|
||||||
|
### Errors
|
||||||
|
|
||||||
|
| Code | Description |
|
||||||
|
| ---- | ----------- |
|
||||||
|
| 400 | Required fields in body were not supplied |
|
||||||
|
| 500 | Error creating the JWT |
|
||||||
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"github.com/dgrijalva/jwt-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
var listen string
|
||||||
|
var secret []byte
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var s string
|
||||||
|
// Parse flags
|
||||||
|
flag.StringVar(&listen, "listen", ":8080", "host and port to listen on")
|
||||||
|
flag.StringVar(&s, "secret", "secret", "JWT secret")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
secret = []byte(s)
|
||||||
|
|
||||||
|
// Routes
|
||||||
|
router := httprouter.New()
|
||||||
|
|
||||||
|
router.POST("/login", Login);
|
||||||
|
|
||||||
|
// Start server
|
||||||
|
log.Printf("starting server on %s", listen)
|
||||||
|
log.Fatal(http.ListenAndServe(listen, router))
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginData struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Client string `json:"client"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Login(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
|
login := LoginData {}
|
||||||
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
err := decoder.Decode(&login)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims {
|
||||||
|
"id": login.ID,
|
||||||
|
"client": login.Client,
|
||||||
|
})
|
||||||
|
|
||||||
|
tokenString, err := token.SignedString(secret)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Write([]byte(tokenString))
|
||||||
|
}
|
Loading…
Reference in New Issue