-
Notifications
You must be signed in to change notification settings - Fork 0
/
claimsheader.go
51 lines (38 loc) · 1.29 KB
/
claimsheader.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
package oioidwsrest
import (
"time"
"net/http"
"encoding/base64"
"encoding/json"
securityprotocol "github.com/KvalitetsIT/gosecurityprotocol"
)
type Claim struct {
Key string `json:"key"`
Value string `json:"value"`
}
func AddExtraClaimsToSessionData(sessionId string, sessionData *securityprotocol.SessionData, r *http.Request) (*securityprotocol.SessionData, error) {
claimsHeaderValue := r.Header.Get(HTTP_HEADER_X_CLAIMS)
if (len(claimsHeaderValue) == 0) {
return sessionData, nil;
}
// Decode and unmarshal
decodedClaimsHeaderValue, err := base64.StdEncoding.DecodeString(claimsHeaderValue)
if (err != nil) {
return nil, err
}
var claims []Claim
err = json.Unmarshal(decodedClaimsHeaderValue, &claims)
if (err != nil) {
return nil, err
}
var resultData *securityprotocol.SessionData
if (sessionData != nil) {
resultData = sessionData
} else {
resultData, _ = securityprotocol.CreateSessionData("", make(map[string][]string), time.Now(), "")
}
for _, claim := range claims {
resultData.AddSessionAttribute(claim.Key, claim.Value)
}
return resultData, nil
}