backend-auth integration
parent
413015945a
commit
cbf2cc6d0c
19
README.md
19
README.md
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
Beep backend records and makes available the last seen times of users.
|
Beep backend records and makes available the last seen times of users.
|
||||||
|
|
||||||
|
**To run this service securely means to run it behind traefik forwarding auth to `backend-auth`**
|
||||||
|
|
||||||
## Environment variables
|
## Environment variables
|
||||||
|
|
||||||
Supply environment variables by either exporting them or editing ```.env```.
|
Supply environment variables by either exporting them or editing ```.env```.
|
||||||
|
@ -45,18 +47,23 @@ An [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) s
|
||||||
### Ping Server
|
### Ping Server
|
||||||
|
|
||||||
```
|
```
|
||||||
POST /ping/:userid/client/:clientid
|
POST /ping
|
||||||
```
|
```
|
||||||
|
|
||||||
Ping the server.
|
Ping the server.
|
||||||
|
|
||||||
#### URL Params
|
#### Required headers
|
||||||
|
|
||||||
| Name | Type | Description | Required |
|
| Name | Description |
|
||||||
| ---- | ---- | ----------- | -------- |
|
| ---- | ----------- |
|
||||||
| userid | String | User's ID. | ✓ |
|
| X-User-Claim | Stringified user claim, populated by `backend-auth` called by `traefik` |
|
||||||
| clientid | String | User's device's ID. | ✓ |
|
|
||||||
|
|
||||||
#### Success Response (200 OK)
|
#### Success Response (200 OK)
|
||||||
|
|
||||||
Empty body.
|
Empty body.
|
||||||
|
|
||||||
|
#### Errors
|
||||||
|
|
||||||
|
| Code | Description |
|
||||||
|
| ---- | ----------- |
|
||||||
|
| 400 | Invalid user claims header. |
|
||||||
|
|
23
main.go
23
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -17,8 +18,8 @@ var listen string
|
||||||
var redisHost string
|
var redisHost string
|
||||||
|
|
||||||
type RawClient struct {
|
type RawClient struct {
|
||||||
UserId string
|
UserId string `json:"userid"`
|
||||||
ClientId string
|
ClientId string `json:"clientid"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var connections map[RawClient][]chan []byte
|
var connections map[RawClient][]chan []byte
|
||||||
|
@ -45,7 +46,7 @@ func main() {
|
||||||
// Routes
|
// Routes
|
||||||
router := httprouter.New()
|
router := httprouter.New()
|
||||||
router.GET("/subscribe/:userid/client/:clientid", Subscribe)
|
router.GET("/subscribe/:userid/client/:clientid", Subscribe)
|
||||||
router.POST("/ping/:userid/client/:clientid", PostTime)
|
router.POST("/ping", PostTime)
|
||||||
|
|
||||||
// Start server
|
// Start server
|
||||||
log.Printf("starting server on %s", listen)
|
log.Printf("starting server on %s", listen)
|
||||||
|
@ -96,11 +97,19 @@ func Subscribe(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Take client data from token
|
|
||||||
func PostTime(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
func PostTime(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
||||||
client := RawClient {
|
ua := r.Header.Get("X-User-Claim")
|
||||||
UserId: p.ByName("userid"),
|
if ua == "" {
|
||||||
ClientId: p.ByName("clientid"),
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var client RawClient
|
||||||
|
err := json.Unmarshal([]byte(ua), &client)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
time := []byte(strconv.FormatInt(time.Now().UTC().Unix(), 10)) // UTC Epoch Time in []byte
|
time := []byte(strconv.FormatInt(time.Now().UTC().Unix(), 10)) // UTC Epoch Time in []byte
|
||||||
|
|
Loading…
Reference in New Issue