/
util.go
98 lines (83 loc) · 2.41 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package util
import (
"encoding/json"
"errors"
"net/http"
"os"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/google/uuid"
)
// Auth contains the unique identifier for a given auth
type Auth struct {
ID uuid.UUID
}
// GetConfig returns a configuration object from decoding the given configuration file
func GetConfig(filePath string) (*Config, error) {
config := Config{}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return nil, err
}
return &config, nil
}
// CreateErrorJSON returns a JSON string containing the key error associated with provided value
func CreateErrorJSON(message string) string {
payload := map[string]string{"error": message}
json, err := json.Marshal(payload)
if err != nil {
return err.Error()
}
return string(json)
}
// ExtractIDFromRequest extracts the parameter provided under parameter ID and converts it into a uuid
func ExtractIDFromRequest(requestParams map[string]string) (uuid.UUID, error) {
id := requestParams["id"]
if len(id) == 0 {
return uuid.Nil, errors.New("No ID provided")
}
return uuid.Parse(id)
}
// ExtractPictureIDFromRequest extracts the parameter provided under parameter picture_id and converts it into a uuid
func ExtractPictureIDFromRequest(requestParams map[string]string) (uuid.UUID, error) {
id := requestParams["picture_id"]
if len(id) == 0 {
return uuid.Nil, errors.New("No Picture ID provided")
}
return uuid.Parse(id)
}
// ExtractAuthIDFromRequest extracts a token from a header of the form `Authorization: Bearer <token>`
func ExtractAuthIDFromRequest(headers http.Header) (*Auth, error) {
authHeader := headers.Get("Authorization")
if len(authHeader) == 0 {
return nil, errors.New("Authorization header not provided")
}
// Extract and parse JWT
rawToken := strings.Replace(authHeader, "Bearer ", "", 1)
jwtParser := jwt.Parser{UseJSONNumber: true}
token, _, err := jwtParser.ParseUnverified(rawToken, jwt.MapClaims{})
if err != nil {
return nil, err
}
// Extract claims from JWT
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
return nil, errors.New("JWT claims are invalid")
}
// Extract ID from JWT claims
id, ok := claims["id"]
if !ok {
return nil, errors.New("JWT does not contain an id")
}
uuid, err := uuid.Parse(id.(string))
if err != nil {
return nil, err
}
return &Auth{uuid}, nil
}