/
callback.go
96 lines (82 loc) · 2.73 KB
/
callback.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
/*
* Copyright 2022-2023 Thorsten A. Knieling
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*/
package auth
import (
"errors"
"fmt"
"plugin"
)
// CallbackInterface callback interface for auth
type CallbackInterface interface {
GetName() string
Init() error
Authenticate(principal PrincipalInterface, userName, passwd string) error
CheckToken(token string, scopes []string) (PrincipalInterface, error)
GenerateToken(IAt string, principal PrincipalInterface) (tokenString string, err error)
}
var callbackList = make([]CallbackInterface, 0)
var callbackMap = make(map[string]CallbackInterface)
// RegisterCallback register callback
func RegisterCallback(callback CallbackInterface) {
callbackList = append(callbackList, callback)
callbackMap[callback.GetName()] = callback
service := &AuthenticationServer{Layer: callback.GetName(), AuthMethod: CallbackMethod}
AuthenticationConfig.AuthenticationServer = append(AuthenticationConfig.AuthenticationServer, service)
}
// ClearCallbacks clear callbacks
func ClearCallbacks() {
callbackList = make([]CallbackInterface, 0)
callbackMap = make(map[string]CallbackInterface)
}
// CallbackInit init login service realm and authorization instances of user using callbacks or plugins
func CallbackInit(auth *AuthenticationServer) error {
if c, ok := callbackMap[auth.Layer]; ok {
return c.Init()
}
return errors.New(auth.Layer + " callback not found")
}
// CallbackAuthenticate authenticate user and password to callback
func CallbackAuthenticate(auth *AuthenticationServer, principal PrincipalInterface, userName, passwd string) error {
principal.AddRoles(DefaultRoles)
if c, ok := callbackMap[auth.Layer]; ok {
return c.Authenticate(principal, userName, passwd)
}
return errors.New(auth.Layer + " callback not found")
}
func checkCallbackToken(token string, scopes []string) (PrincipalInterface, error) {
for _, c := range callbackMap {
return c.CheckToken(token, scopes)
}
return nil, errors.New("no callback validates token")
}
func generateCallbackToken(IAt string, principal PrincipalInterface) (tokenString string, err error) {
for _, c := range callbackMap {
t, err := c.GenerateToken(IAt, principal)
if err == nil {
return t, err
}
}
return "", errors.New("no callback generates token")
}
// RegisterPlugin register plugin
func RegisterPlugin() {
}
func loadPlugin(mod string) (*plugin.Plugin, error) {
fmt.Println("Loading plugin ... " + mod)
// load module
// 1. open the so file to load the symbols
plug, err := plugin.Open(mod)
if err != nil {
fmt.Println(err)
return nil, err
}
return plug, nil
}