github.com/greenpau/go-authcrunch@v1.1.4/pkg/idp/provider.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 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 idp 16 17 import ( 18 "encoding/json" 19 // "fmt" 20 "github.com/greenpau/go-authcrunch/pkg/authn/enums/operator" 21 "github.com/greenpau/go-authcrunch/pkg/authn/icons" 22 "github.com/greenpau/go-authcrunch/pkg/errors" 23 "github.com/greenpau/go-authcrunch/pkg/idp/oauth" 24 "github.com/greenpau/go-authcrunch/pkg/idp/saml" 25 "github.com/greenpau/go-authcrunch/pkg/requests" 26 "go.uber.org/zap" 27 ) 28 29 // IdentityProvider represents identity provider. 30 type IdentityProvider interface { 31 GetRealm() string 32 GetName() string 33 GetKind() string 34 GetDriver() string 35 GetConfig() map[string]interface{} 36 Configure() error 37 Configured() bool 38 Request(operator.Type, *requests.Request) error 39 GetLoginIcon() *icons.LoginIcon 40 GetLogoutURL() string 41 GetIdentityTokenCookieName() string 42 } 43 44 // NewIdentityProvider returns IdentityProvider instance. 45 func NewIdentityProvider(cfg *IdentityProviderConfig, logger *zap.Logger) (IdentityProvider, error) { 46 var p IdentityProvider 47 var err error 48 49 if logger == nil { 50 return nil, errors.ErrIdentityProviderConfigureLoggerNotFound 51 } 52 53 if err := cfg.Validate(); err != nil { 54 return nil, err 55 } 56 57 b, _ := json.Marshal(cfg.Params) 58 59 switch cfg.Kind { 60 case "oauth": 61 config := &oauth.Config{} 62 if err := json.Unmarshal(b, config); err != nil { 63 return nil, errors.ErrIdentityProviderNewConfig.WithArgs(cfg.Params, err) 64 } 65 config.Name = cfg.Name 66 p, err = oauth.NewIdentityProvider(config, logger) 67 case "saml": 68 config := &saml.Config{} 69 if err := json.Unmarshal(b, config); err != nil { 70 return nil, errors.ErrIdentityProviderNewConfig.WithArgs(cfg.Params, err) 71 } 72 config.Name = cfg.Name 73 p, err = saml.NewIdentityProvider(config, logger) 74 } 75 76 if err != nil { 77 return nil, err 78 } 79 80 return p, nil 81 }