-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
68 lines (58 loc) · 1.39 KB
/
common.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
package oauthclient
import (
"context"
"encoding/json"
"fmt"
"os"
"github.com/shieldoo/shieldoo-mesh-oauth/model"
"github.com/shieldoo/shieldoo-mesh-oauth/utils"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
var _cfg *utils.Config
func Init(cfg *utils.Config) {
_cfg = cfg
InitMicrosoft(cfg)
InitGoogle(cfg)
var err error
if err != nil {
log.Panic("Unable initialize OauthClient: ", err)
os.Exit(1000)
}
}
type Jwks struct {
Keys []JSONWebKeys `json:"keys"`
}
type JSONWebKeys struct {
Kty string `json:"kty"`
Kid string `json:"kid"`
Use string `json:"use"`
N string `json:"n"`
E string `json:"e"`
X5c []string `json:"x5c"`
}
func exchangeCode(code string, oauthConfig *oauth2.Config) (*oauth2.Token, error) {
token, err := oauthConfig.Exchange(context.Background(), code)
if err != nil {
log.Error("code exchange failed: ", err.Error())
return nil, err
}
return token, nil
}
func extractIdToken(tokenResponse *oauth2.Token) (string, error) {
idToken, ok := tokenResponse.Extra("id_token").(string)
if !ok {
log.Error("no id_token field in oauth2 token")
return "", fmt.Errorf("no id_token field in oauth2 token")
}
return idToken, nil
}
func decodeParams(state string) (*model.Params, error) {
var params model.Params
err := json.Unmarshal([]byte(state), ¶ms)
if err != nil {
log.Error(err)
return nil, err
}
return ¶ms, nil
}