-
Notifications
You must be signed in to change notification settings - Fork 1
/
sso.go
42 lines (33 loc) · 916 Bytes
/
sso.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
package authentication
import (
"context"
"fmt"
rest "github.com/SKF/go-rest-utility/client"
"github.com/SKF/go-utility/v2/stages"
)
type SSOClient struct {
*rest.Client
}
func NewSSOClient(stage string) *SSOClient {
baseURL := rest.WithBaseURL(fmt.Sprintf("https://sso-api.%s.users.enlight.skf.com", stage))
if stage == stages.StageProd {
baseURL = rest.WithBaseURL("https://sso-api.users.enlight.skf.com")
}
return &SSOClient{
Client: rest.NewClient(baseURL),
}
}
func (c *SSOClient) getUserIDFromAccessToken(ctx context.Context, accessToken string) (string, error) {
request := rest.Get("users/me").
SetHeader("Authorization", accessToken).
SetHeader("Accept", "application/json")
var response struct {
Data struct {
ID string `json:"id"`
} `json:"data"`
}
if err := c.DoAndUnmarshal(ctx, request, &response); err != nil {
return "", err
}
return response.Data.ID, nil
}