2021-08-12 12:49:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-08-20 22:33:43 +02:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2021-08-12 12:49:10 +02:00
|
|
|
"github.com/golang-jwt/jwt"
|
|
|
|
"net/http"
|
2021-08-20 22:33:43 +02:00
|
|
|
"time"
|
2021-08-12 12:49:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// extractAccountIdFromRequestContext extracts accountId from the request context previously filled by the JWT token (after auth)
|
|
|
|
func extractAccountIdFromRequestContext(r *http.Request) string {
|
|
|
|
token := r.Context().Value("user").(*jwt.Token)
|
|
|
|
claims := token.Claims.(jwt.MapClaims)
|
|
|
|
|
|
|
|
//actually a user id but for now we have a 1 to 1 mapping.
|
|
|
|
return claims["sub"].(string)
|
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-08-23 21:43:05 +02:00
|
|
|
//writeJSONObject simply writes object to the HTTP reponse in JSON format
|
|
|
|
func writeJSONObject(w http.ResponseWriter, obj interface{}) {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
|
|
err := json.NewEncoder(w).Encode(obj)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "failed handling request", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
//Duration is used strictly for JSON requests/responses due to duration marshalling issues
|
|
|
|
type Duration struct {
|
|
|
|
time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Duration) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(d.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Duration) UnmarshalJSON(b []byte) error {
|
|
|
|
var v interface{}
|
|
|
|
if err := json.Unmarshal(b, &v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch value := v.(type) {
|
|
|
|
case float64:
|
|
|
|
d.Duration = time.Duration(value)
|
|
|
|
return nil
|
|
|
|
case string:
|
|
|
|
var err error
|
|
|
|
d.Duration, err = time.ParseDuration(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return errors.New("invalid duration")
|
|
|
|
}
|
|
|
|
}
|