|
| 1 | +// Copyright 2021 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package model |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "fmt" |
| 20 | +) |
| 21 | + |
| 22 | +// AuthType ... |
| 23 | +type AuthType string |
| 24 | + |
| 25 | +const ( |
| 26 | + // AuthTypeBasic ... |
| 27 | + AuthTypeBasic AuthType = "basic" |
| 28 | + // AuthTypeBearer ... |
| 29 | + AuthTypeBearer AuthType = "bearer" |
| 30 | + // AuthTypeOAuth2 ... |
| 31 | + AuthTypeOAuth2 AuthType = "oauth2" |
| 32 | +) |
| 33 | + |
| 34 | +// GrantType ... |
| 35 | +type GrantType string |
| 36 | + |
| 37 | +const ( |
| 38 | + // GrantTypePassword ... |
| 39 | + GrantTypePassword GrantType = "password" |
| 40 | + // GrantTypeClientCredentials ... |
| 41 | + GrantTypeClientCredentials GrantType = "clientCredentials" |
| 42 | + // GrantTypeTokenExchange ... |
| 43 | + GrantTypeTokenExchange GrantType = "tokenExchange" |
| 44 | +) |
| 45 | + |
| 46 | +// authTypesMapping map to support JSON unmarshalling when guessing the auth scheme |
| 47 | +var authTypesMapping = map[AuthType]AuthProperties{ |
| 48 | + AuthTypeBasic: &BasicAuthProperties{}, |
| 49 | + AuthTypeBearer: &BearerAuthProperties{}, |
| 50 | + AuthTypeOAuth2: &OAuth2AuthProperties{}, |
| 51 | +} |
| 52 | + |
| 53 | +// Auth ... |
| 54 | +type Auth struct { |
| 55 | + // Name Unique auth definition name |
| 56 | + Name string `json:"name" validate:"required"` |
| 57 | + // Scheme Defines the auth type |
| 58 | + Scheme AuthType `json:"scheme,omitempty" validate:"omitempty,min=1"` |
| 59 | + // Properties ... |
| 60 | + Properties AuthProperties `json:"properties" validate:"required"` |
| 61 | +} |
| 62 | + |
| 63 | +// UnmarshalJSON ... |
| 64 | +func (a *Auth) UnmarshalJSON(data []byte) error { |
| 65 | + auth := make(map[string]json.RawMessage) |
| 66 | + if err := json.Unmarshal(data, &auth); err != nil { |
| 67 | + // it's a file |
| 68 | + file, err := unmarshalFile(data) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + // call us recursively |
| 73 | + if err := json.Unmarshal(file, &a); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + return nil |
| 77 | + } |
| 78 | + if err := unmarshalKey("scheme", auth, &a.Scheme); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + if err := unmarshalKey("name", auth, &a.Name); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + |
| 85 | + if len(a.Scheme) == 0 { |
| 86 | + a.Scheme = AuthTypeBasic |
| 87 | + } |
| 88 | + if _, ok := authTypesMapping[a.Scheme]; !ok { |
| 89 | + return fmt.Errorf("authentication scheme %s not supported", authTypesMapping["type"]) |
| 90 | + } |
| 91 | + // we take the type we want to unmarshal based on the scheme |
| 92 | + authProperties := authTypesMapping[a.Scheme] |
| 93 | + if err := unmarshalKey("properties", auth, authProperties); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// AuthProperties ... |
| 100 | +type AuthProperties interface { |
| 101 | + // GetMetadata ... |
| 102 | + GetMetadata() *Metadata |
| 103 | + // GetSecret ... |
| 104 | + GetSecret() string |
| 105 | +} |
| 106 | + |
| 107 | +// BaseAuthProperties ... |
| 108 | +type BaseAuthProperties struct { |
| 109 | + Common |
| 110 | + // Secret Expression referencing a workflow secret that contains all needed auth info |
| 111 | + Secret string `json:"secret,omitempty"` |
| 112 | +} |
| 113 | + |
| 114 | +// UnmarshalJSON ... |
| 115 | +func (b *BaseAuthProperties) UnmarshalJSON(data []byte) error { |
| 116 | + properties := make(map[string]json.RawMessage) |
| 117 | + if err := json.Unmarshal(data, &properties); err != nil { |
| 118 | + b.Secret, err = unmarshalString(data) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + return nil |
| 123 | + } |
| 124 | + if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + if err := unmarshalKey("secret", properties, &b.Secret); err != nil { |
| 128 | + return err |
| 129 | + } |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +// GetMetadata ... |
| 134 | +func (b *BaseAuthProperties) GetMetadata() *Metadata { |
| 135 | + return &b.Metadata |
| 136 | +} |
| 137 | + |
| 138 | +// GetSecret ... |
| 139 | +func (b *BaseAuthProperties) GetSecret() string { |
| 140 | + return b.Secret |
| 141 | +} |
| 142 | + |
| 143 | +// BasicAuthProperties Basic Auth Info |
| 144 | +type BasicAuthProperties struct { |
| 145 | + BaseAuthProperties |
| 146 | + // Username String or a workflow expression. Contains the username |
| 147 | + Username string `json:"username" validate:"required"` |
| 148 | + // Password String or a workflow expression. Contains the user password |
| 149 | + Password string `json:"password" validate:"required"` |
| 150 | +} |
| 151 | + |
| 152 | +// UnmarshalJSON ... |
| 153 | +func (b *BasicAuthProperties) UnmarshalJSON(data []byte) error { |
| 154 | + properties := make(map[string]json.RawMessage) |
| 155 | + if err := json.Unmarshal(data, &properties); err != nil { |
| 156 | + err = json.Unmarshal(data, &b.BaseAuthProperties) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + return nil |
| 161 | + } |
| 162 | + if err := unmarshalKey("username", properties, &b.Username); err != nil { |
| 163 | + return err |
| 164 | + } |
| 165 | + if err := unmarshalKey("password", properties, &b.Password); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +// BearerAuthProperties Bearer auth information |
| 175 | +type BearerAuthProperties struct { |
| 176 | + BaseAuthProperties |
| 177 | + // Token String or a workflow expression. Contains the token |
| 178 | + Token string `json:"token" validate:"required"` |
| 179 | +} |
| 180 | + |
| 181 | +// UnmarshalJSON ... |
| 182 | +func (b *BearerAuthProperties) UnmarshalJSON(data []byte) error { |
| 183 | + properties := make(map[string]json.RawMessage) |
| 184 | + if err := json.Unmarshal(data, &properties); err != nil { |
| 185 | + err = json.Unmarshal(data, &b.BaseAuthProperties) |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + return nil |
| 190 | + } |
| 191 | + if err := unmarshalKey("token", properties, &b.Token); err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil { |
| 195 | + return err |
| 196 | + } |
| 197 | + return nil |
| 198 | +} |
| 199 | + |
| 200 | +// OAuth2AuthProperties OAuth2 information |
| 201 | +type OAuth2AuthProperties struct { |
| 202 | + BaseAuthProperties |
| 203 | + // Authority String or a workflow expression. Contains the authority information |
| 204 | + Authority string `json:"authority,omitempty" validate:"omitempty,min=1"` |
| 205 | + // GrantType Defines the grant type |
| 206 | + GrantType GrantType `json:"grantType" validate:"required"` |
| 207 | + // ClientID String or a workflow expression. Contains the client identifier |
| 208 | + ClientID string `json:"clientId" validate:"required"` |
| 209 | + // ClientSecret Workflow secret or a workflow expression. Contains the client secret |
| 210 | + ClientSecret string `json:"clientSecret,omitempty" validate:"omitempty,min=1"` |
| 211 | + // Scopes Array containing strings or workflow expressions. Contains the OAuth2 scopes |
| 212 | + Scopes []string `json:"scopes,omitempty" validate:"omitempty,min=1"` |
| 213 | + // Username String or a workflow expression. Contains the username. Used only if grantType is 'resourceOwner' |
| 214 | + Username string `json:"username,omitempty" validate:"omitempty,min=1"` |
| 215 | + // Password String or a workflow expression. Contains the user password. Used only if grantType is 'resourceOwner' |
| 216 | + Password string `json:"password,omitempty" validate:"omitempty,min=1"` |
| 217 | + // Audiences Array containing strings or workflow expressions. Contains the OAuth2 audiences |
| 218 | + Audiences []string `json:"audiences,omitempty" validate:"omitempty,min=1"` |
| 219 | + // SubjectToken String or a workflow expression. Contains the subject token |
| 220 | + SubjectToken string `json:"subjectToken,omitempty" validate:"omitempty,min=1"` |
| 221 | + // RequestedSubject String or a workflow expression. Contains the requested subject |
| 222 | + RequestedSubject string `json:"requestedSubject,omitempty" validate:"omitempty,min=1"` |
| 223 | + // RequestedIssuer String or a workflow expression. Contains the requested issuer |
| 224 | + RequestedIssuer string `json:"requestedIssuer,omitempty" validate:"omitempty,min=1"` |
| 225 | +} |
| 226 | + |
| 227 | +// TODO: use reflection to unmarshal the keys and think on a generic approach to handle them |
| 228 | + |
| 229 | +// UnmarshalJSON ... |
| 230 | +func (b *OAuth2AuthProperties) UnmarshalJSON(data []byte) error { |
| 231 | + properties := make(map[string]json.RawMessage) |
| 232 | + if err := json.Unmarshal(data, &properties); err != nil { |
| 233 | + err = json.Unmarshal(data, &b.BaseAuthProperties) |
| 234 | + if err != nil { |
| 235 | + return err |
| 236 | + } |
| 237 | + return nil |
| 238 | + } |
| 239 | + if err := unmarshalKey("authority", properties, &b.Authority); err != nil { |
| 240 | + return err |
| 241 | + } |
| 242 | + if err := unmarshalKey("grantType", properties, &b.GrantType); err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + if err := unmarshalKey("clientId", properties, &b.ClientID); err != nil { |
| 246 | + return err |
| 247 | + } |
| 248 | + if err := unmarshalKey("clientSecret", properties, &b.ClientSecret); err != nil { |
| 249 | + return err |
| 250 | + } |
| 251 | + if err := unmarshalKey("scopes", properties, &b.Scopes); err != nil { |
| 252 | + return err |
| 253 | + } |
| 254 | + if err := unmarshalKey("username", properties, &b.Username); err != nil { |
| 255 | + return err |
| 256 | + } |
| 257 | + if err := unmarshalKey("password", properties, &b.Password); err != nil { |
| 258 | + return err |
| 259 | + } |
| 260 | + if err := unmarshalKey("audiences", properties, &b.Audiences); err != nil { |
| 261 | + return err |
| 262 | + } |
| 263 | + if err := unmarshalKey("subjectToken", properties, &b.SubjectToken); err != nil { |
| 264 | + return err |
| 265 | + } |
| 266 | + if err := unmarshalKey("requestedSubject", properties, &b.RequestedSubject); err != nil { |
| 267 | + return err |
| 268 | + } |
| 269 | + if err := unmarshalKey("requestedIssuer", properties, &b.RequestedIssuer); err != nil { |
| 270 | + return err |
| 271 | + } |
| 272 | + if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil { |
| 273 | + return err |
| 274 | + } |
| 275 | + return nil |
| 276 | +} |
0 commit comments