|
2 | 2 | package entity
|
3 | 3 |
|
4 | 4 | import (
|
| 5 | + "encoding/json" |
5 | 6 | "errors"
|
| 7 | + "strings" |
6 | 8 |
|
7 | 9 | "github.com/forest33/warthog/pkg/structs"
|
8 | 10 | )
|
@@ -42,6 +44,23 @@ type WorkspaceItemServer struct {
|
42 | 44 | ClientCertificate string `json:"client_certificate,omitempty"`
|
43 | 45 | ClientKey string `json:"client_key,omitempty"`
|
44 | 46 | Request map[string]map[string]*SavedQuery `json:"request"`
|
| 47 | + Auth *Auth `json:"auth"` |
| 48 | +} |
| 49 | + |
| 50 | +// Auth authentication data |
| 51 | +type Auth struct { |
| 52 | + Type string `json:"type,omitempty"` |
| 53 | + Login string `json:"login,omitempty"` |
| 54 | + Password string `json:"password,omitempty"` |
| 55 | + Token string `json:"token,omitempty"` |
| 56 | + Algorithm string `json:"algorithm,omitempty"` |
| 57 | + Secret string `json:"secret,omitempty"` |
| 58 | + PrivateKey string `json:"private_key,omitempty"` |
| 59 | + SecretBase64 bool `json:"secret_base64,omitempty"` |
| 60 | + HeaderPrefix string `json:"header_prefix,omitempty"` |
| 61 | + Payload map[string]interface{} `json:"payload,omitempty"` |
| 62 | + GoogleScopes []string `json:"google_scopes,omitempty"` |
| 63 | + GoogleToken string `json:"google_token,omitempty"` |
45 | 64 | }
|
46 | 65 |
|
47 | 66 | // Model creates ServerRequest from UI request
|
@@ -122,6 +141,62 @@ func (s *WorkspaceItemServer) Model(server map[string]interface{}) error {
|
122 | 141 | if v, ok := server["client_key"]; ok && v != nil {
|
123 | 142 | s.ClientKey = v.(string)
|
124 | 143 | }
|
| 144 | + if v, ok := server["auth"]; ok && v != nil { |
| 145 | + s.Auth = &Auth{} |
| 146 | + if err := s.Auth.Model(v.(map[string]interface{})); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +// Model creates Auth from UI request |
| 155 | +func (s *Auth) Model(auth map[string]interface{}) error { |
| 156 | + authType, ok := auth["type"] |
| 157 | + if !ok || authType == AuthTypeNone { |
| 158 | + s.Type = AuthTypeNone |
| 159 | + return nil |
| 160 | + } |
| 161 | + |
| 162 | + s.Type = authType.(string) |
| 163 | + |
| 164 | + if v, ok := auth["login"]; ok { |
| 165 | + s.Login = v.(string) |
| 166 | + } |
| 167 | + if v, ok := auth["password"]; ok { |
| 168 | + s.Password = v.(string) |
| 169 | + } |
| 170 | + if v, ok := auth["token"]; ok { |
| 171 | + s.Token = v.(string) |
| 172 | + } |
| 173 | + if v, ok := auth["algorithm"]; ok { |
| 174 | + s.Algorithm = v.(string) |
| 175 | + } |
| 176 | + if v, ok := auth["secret"]; ok { |
| 177 | + s.Secret = v.(string) |
| 178 | + } |
| 179 | + if v, ok := auth["private_key"]; ok { |
| 180 | + s.PrivateKey = v.(string) |
| 181 | + } |
| 182 | + if v, ok := auth["secret_base64"]; ok { |
| 183 | + s.SecretBase64 = v.(bool) |
| 184 | + } |
| 185 | + if v, ok := auth["header_prefix"]; ok { |
| 186 | + s.HeaderPrefix = strings.TrimSpace(v.(string)) |
| 187 | + } |
| 188 | + if v, ok := auth["payload"]; ok { |
| 189 | + s.Payload = map[string]interface{}{} |
| 190 | + if err := json.Unmarshal([]byte(v.(string)), &s.Payload); err != nil { |
| 191 | + return err |
| 192 | + } |
| 193 | + } |
| 194 | + if v, ok := auth["google_token"]; ok { |
| 195 | + s.GoogleToken = v.(string) |
| 196 | + } |
| 197 | + if v, ok := auth["google_scopes"]; ok { |
| 198 | + s.GoogleScopes = strings.Split(v.(string), ",") |
| 199 | + } |
125 | 200 |
|
126 | 201 | return nil
|
127 | 202 | }
|
0 commit comments