-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
55 lines (43 loc) · 1.56 KB
/
auth.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
package auth
import (
"fmt"
"github.com/goal-web/contracts"
"github.com/goal-web/supports/exceptions"
"github.com/goal-web/supports/utils"
)
type Auth struct {
authConfig Config
guardDrivers map[string]contracts.GuardDriver
userProviders map[string]contracts.UserProvider
userDrivers map[string]contracts.UserProviderDriver
}
func (this *Auth) ExtendUserProvider(key string, provider contracts.UserProviderDriver) {
this.userDrivers[key] = provider
}
func (this *Auth) ExtendGuard(key string, guard contracts.GuardDriver) {
this.guardDrivers[key] = guard
}
func (this *Auth) Guard(key string, ctx contracts.Context) contracts.Guard {
config := this.authConfig.Guards[key]
driver := utils.GetStringField(config, "driver")
if guardDriver, existsDriver := this.guardDrivers[driver]; existsDriver {
return guardDriver(key, config, ctx, this.UserProvider(utils.GetStringField(config, "provider")))
}
panic(GuardException{
Exception: exceptions.New(fmt.Sprintf("unsupported guard driver:%s", driver), config),
})
}
func (this *Auth) UserProvider(key string) contracts.UserProvider {
if userProvider, existsUserProvider := this.userProviders[key]; existsUserProvider {
return userProvider
}
config := this.authConfig.Users[key]
driver := utils.GetStringField(config, "driver")
if userDriver, existsProvider := this.userDrivers[driver]; existsProvider {
this.userProviders[key] = userDriver(config)
return this.userProviders[key]
}
panic(UserProviderException{
Exception: exceptions.New(fmt.Sprintf("unsupported user driver:%s", driver), config),
})
}