-
Notifications
You must be signed in to change notification settings - Fork 3
/
identity_def.go
73 lines (56 loc) · 1.38 KB
/
identity_def.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
package eventline
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"time"
"github.com/exograd/go-daemon/check"
)
type UnknownIdentityDefError struct {
Connector string
Type string
}
func (err UnknownIdentityDefError) Error() string {
return fmt.Sprintf("unknown identity %q in connector %q",
err.Type, err.Connector)
}
type IdentityDef struct {
Type string
DeferredReadiness bool
Refreshable bool
Data IdentityData
DataDef *IdentityDataDef // used when there is no actual identity
}
type IdentityData interface {
check.Object
Def() *IdentityDataDef
Environment() map[string]string
}
type OAuth2IdentityData interface {
IdentityData
RedirectionURI(*http.Client, string, string) (string, error)
FetchTokenData(*http.Client, string, string) error
}
type RefreshableOAuth2IdentityData interface {
OAuth2IdentityData
Refresh(*http.Client) error
RefreshTime() time.Time
}
func NewIdentityDef(typeName string, dataValue IdentityData) *IdentityDef {
return &IdentityDef{
Type: typeName,
Data: dataValue,
}
}
func (idef *IdentityDef) IsOAuth2() bool {
_, ok := idef.Data.(OAuth2IdentityData)
return ok
}
func (idef *IdentityDef) DecodeData(data []byte) (IdentityData, error) {
idata := reflect.New(reflect.TypeOf(idef.Data).Elem()).Interface()
if err := json.Unmarshal(data, idata); err != nil {
return nil, err
}
return idata.(IdentityData), nil
}