-
Notifications
You must be signed in to change notification settings - Fork 81
/
model_auth.go
82 lines (72 loc) 路 3.31 KB
/
model_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
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
// Copyright 2022 The kubegems.io Authors
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"time"
)
type AuthSource struct {
ID uint `json:"id"`
Name string `gorm:"unique" json:"name"`
Kind string `json:"kind" binding:"oneof=LDAP OAUTH"`
Vendor string `gorm:"type:varchar(30)" json:"vendor" binding:"omitempty,oneof=github gitlab oauth ldap"`
Config AuthSourceConfig `json:"config" binding:"required,json"`
TokenType string `json:"tokenType" binding:"required,oneof=Bearer"`
Enabled bool `json:"enabled"`
CreatedAt *time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
}
type AuthSourceSimple struct {
ID uint `json:"id"`
Name string `gorm:"unique" json:"name"`
Kind string `json:"kind"`
Enabled bool `json:"enabled"`
Vendor string `json:"vendor"`
}
func (AuthSourceSimple) TableName() string {
return "auth_sources"
}
type AuthSourceConfig struct {
AuthURL string `json:"authURL,omitempty" binding:"omitempty,url,required_with=TokenURL UserInfoURL RedirectURL AppID AppSecret"`
TokenURL string `json:"tokenURL,omitempty" binding:"omitempty,url,required_with=AuthURL UserInfoURL RedirectURL AppID AppSecret"`
UserInfoURL string `json:"userInfoURL,omitempty" binding:"omitempty,url,required_with=AuthURL TokenURL RedirectURL AppID AppSecret"`
RedirectURL string `json:"redirectURL,omitempty" binding:"omitempty,url,required_with=AuthURL TokenURL UserInfoURL AppID AppSecret"`
AppID string `json:"appID,omitempty" binding:"required_with=AuthURL TokenURL UserInfoURL RedirectURL AppSecret"`
AppSecret string `json:"appSecret,omitempty" binding:"required_with=AuthURL TokenURL UserInfoURL RedirectURL AppID"`
Scopes []string `json:"scopes,omitempty"`
// ldap
Name string `json:"name,omitempty"`
LdapAddr string `json:"ldapaddr,omitempty" binding:"omitempty,hostname_port,required_with=BaseDN BindUsername BindPassword"`
BaseDN string `json:"basedn,omitempty" binding:"required_with=LdapAddr BindUsername BindPassword"`
EnableTLS bool `json:"enableTLS,omitempty"`
Filter string `json:"filter,omitempty"`
BindUsername string `json:"binduser,omitempty" binding:"required_with=LdapAddr BaseDN BindPassword"`
BindPassword string `json:"password,omitempty" binding:"required_with=LdapAddr BaseDN BindUsername"`
}
func (cfg *AuthSourceConfig) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
result := AuthSourceConfig{}
err := json.Unmarshal(bytes, &result)
*cfg = result
return err
}
func (cfg AuthSourceConfig) Value() (driver.Value, error) {
return json.Marshal(cfg)
}