github.com/greenpau/go-authcrunch@v1.1.4/pkg/authn/icons/icon.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 icons
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // LoginIcon represents an icon associated with an identity store or provider.
    25  type LoginIcon struct {
    26  	Realm               string `json:"realm,omitempty" xml:"realm,omitempty" yaml:"realm,omitempty"`
    27  	Endpoint            string `json:"endpoint,omitempty" xml:"endpoint,omitempty" yaml:"endpoint,omitempty"`
    28  	Priority            int    `json:"priority,omitempty" xml:"priority,omitempty" yaml:"priority,omitempty"`
    29  	ClassName           string `json:"class_name,omitempty" xml:"class_name,omitempty" yaml:"class_name,omitempty"`
    30  	Color               string `json:"color,omitempty" xml:"color,omitempty" yaml:"color,omitempty"`
    31  	BackgroundColor     string `json:"background_color,omitempty" xml:"background_color,omitempty" yaml:"background_color,omitempty"`
    32  	Text                string `json:"text,omitempty" xml:"text,omitempty" yaml:"text,omitempty"`
    33  	TextColor           string `json:"text_color,omitempty" xml:"text_color,omitempty" yaml:"text_color,omitempty"`
    34  	TextBackgroundColor string `json:"text_background_color,omitempty" xml:"text_background_color,omitempty" yaml:"text_background_color,omitempty"`
    35  
    36  	// RegistrationEnabled controls whether visitors can registers.
    37  	RegistrationEnabled bool `json:"registration_enabled,omitempty" xml:"registration_enabled,omitempty" yaml:"registration_enabled,omitempty"`
    38  	// UsernameRecoveryEnabled controls whether a user could recover username by providing an email address.
    39  	UsernameRecoveryEnabled bool `json:"username_recovery_enabled,omitempty" xml:"username_recovery_enabled,omitempty" yaml:"username_recovery_enabled,omitempty"`
    40  	// PasswordRecoveryEnabled controls whether a user could recover password by providing an email address.
    41  	PasswordRecoveryEnabled bool `json:"password_recovery_enabled,omitempty" xml:"password_recovery_enabled,omitempty" yaml:"password_recovery_enabled,omitempty"`
    42  	// ContactSupportEnabled controls whether contact support link is available.
    43  	ContactSupportEnabled bool `json:"contact_support_enabled,omitempty" xml:"contact_support_enabled,omitempty" yaml:"contact_support_enabled,omitempty"`
    44  
    45  	// SupportLink is the link to the support portal.
    46  	SupportLink string `json:"support_link,omitempty" xml:"support_link,omitempty" yaml:"support_link,omitempty"`
    47  	// SupportEmail is the email address to reach support.
    48  	SupportEmail string `json:"support_email,omitempty" xml:"support_email,omitempty" yaml:"support_email,omitempty"`
    49  }
    50  
    51  // NewLoginIcon returns an instance of LoginIcon.
    52  func NewLoginIcon(s string) *LoginIcon {
    53  	icon := &LoginIcon{}
    54  	icon.Configure(s)
    55  	return icon
    56  }
    57  
    58  // Configure sets defaults for an instance of LoginIcon.
    59  func (icon *LoginIcon) Configure(s string) {
    60  	if icon.ClassName == "" {
    61  		switch s {
    62  		case "google":
    63  			icon.ClassName = "lab la-google la-2x"
    64  		case "facebook":
    65  			icon.ClassName = "lab la-facebook la-2x"
    66  		case "linkedin":
    67  			icon.ClassName = "lab la-linkedin la-2x"
    68  		case "github":
    69  			icon.ClassName = "lab la-github la-2x"
    70  		case "gitlab":
    71  			icon.ClassName = "lab la-gitlab la-2x"
    72  		case "discord":
    73  			icon.ClassName = "lab la-discord la-2x"
    74  		case "windows":
    75  			icon.ClassName = "lab la-windows la-2x"
    76  		case "azure":
    77  			icon.ClassName = "lab la-windows la-2x"
    78  		case "aws", "cognito":
    79  			icon.ClassName = "lab la-aws la-2x"
    80  		case "amazon":
    81  			icon.ClassName = "lab la-amazon la-2x"
    82  		case "ldap":
    83  			icon.ClassName = "las la-shield-alt la-2x"
    84  		case "local":
    85  			icon.ClassName = "las la-key la-2x"
    86  		default:
    87  			icon.ClassName = "lab la-codepen la-2x"
    88  		}
    89  	}
    90  
    91  	if icon.Color == "" {
    92  		switch s {
    93  		// case "google":
    94  		// case "facebook":
    95  		// case "linkedin":
    96  		// case "gitlab":
    97  		// case "windows":
    98  		// case "azure":
    99  		// case "aws", "cognito":
   100  		// case "amazon":
   101  		case "github":
   102  			icon.Color = "#f6f8fa"
   103  		case "ldap":
   104  			icon.Color = "white"
   105  		case "local":
   106  			icon.Color = "white"
   107  		default:
   108  			icon.Color = "white"
   109  		}
   110  	}
   111  
   112  	if icon.BackgroundColor == "" {
   113  		switch s {
   114  		case "google":
   115  			// MaterializeCSS "red darken-1"
   116  			icon.BackgroundColor = "#e53935"
   117  		case "facebook":
   118  			// MaterializeCSS "blue darken-4"
   119  			icon.BackgroundColor = "#0d47a1"
   120  		case "linkedin":
   121  			// MaterializeCSS "blue darken-1"
   122  			icon.BackgroundColor = "#1e88e5"
   123  		case "gitlab":
   124  			icon.BackgroundColor = "#fc6d26"
   125  		case "github":
   126  			icon.BackgroundColor = "#24292f"
   127  		case "discord":
   128  			icon.BackgroundColor = "#5865f2"
   129  		case "windows":
   130  			// MaterializeCSS "orange darken-1"
   131  			icon.BackgroundColor = "#fb8c00"
   132  		case "azure":
   133  			// MaterializeCSS "blue"
   134  			icon.BackgroundColor = "#03a9f4"
   135  		case "aws", "amazon", "cognito":
   136  			// MaterializeCSS "blue-grey darken-2"
   137  			icon.BackgroundColor = "#455a64"
   138  		case "ldap":
   139  			icon.BackgroundColor = "#324960"
   140  		case "local":
   141  			icon.BackgroundColor = "#324960"
   142  		default:
   143  			icon.BackgroundColor = "#324960"
   144  		}
   145  	}
   146  
   147  	if icon.Text == "" {
   148  		switch s {
   149  		case "google":
   150  			icon.Text = "Google"
   151  		case "facebook":
   152  			icon.Text = "Facebook"
   153  		case "linkedin":
   154  			icon.Text = "LinkedIn"
   155  		case "github":
   156  			icon.Text = "Github"
   157  		case "discord":
   158  			icon.Text = "Discord"
   159  		case "windows":
   160  			icon.Text = "Microsoft"
   161  		case "azure":
   162  			icon.Text = "Azure"
   163  		case "aws", "cognito":
   164  			icon.Text = "AWS"
   165  		case "amazon":
   166  			icon.Text = "Amazon"
   167  		default:
   168  			icon.Text = icon.Realm
   169  		}
   170  	}
   171  
   172  	if icon.TextColor == "" {
   173  		switch s {
   174  		default:
   175  			icon.TextColor = "#37474f"
   176  		}
   177  	}
   178  
   179  	if icon.BackgroundColor == "" {
   180  		switch s {
   181  		case "google":
   182  			// MaterializeCSS "red darken-1"
   183  			icon.BackgroundColor = "#e53935"
   184  		case "facebook":
   185  			// MaterializeCSS "blue darken-4"
   186  			icon.BackgroundColor = "#0d47a1"
   187  		case "linkedin":
   188  			// MaterializeCSS "blue darken-1"
   189  			icon.BackgroundColor = "#1e88e5"
   190  		case "github":
   191  			icon.BackgroundColor = "#24292f"
   192  		case "windows":
   193  			// MaterializeCSS "orange darken-1"
   194  			icon.BackgroundColor = "#fb8c00"
   195  		case "discord":
   196  			icon.BackgroundColor = "#5865f2"
   197  		case "azure":
   198  			// MaterializeCSS "blue"
   199  			icon.BackgroundColor = "#03a9f4"
   200  		case "aws", "amazon", "cognito":
   201  			// MaterializeCSS "blue-grey darken-2"
   202  			icon.BackgroundColor = "#455a64"
   203  		case "ldap":
   204  			icon.BackgroundColor = "#324960"
   205  		case "local":
   206  			icon.BackgroundColor = "#324960"
   207  		default:
   208  			icon.BackgroundColor = "#324960"
   209  		}
   210  	}
   211  
   212  	return
   213  }
   214  
   215  // SetRealm sets the realm associated with the icon.
   216  func (icon *LoginIcon) SetRealm(s string) {
   217  	icon.Realm = s
   218  	if icon.Text == "" {
   219  		icon.Text = strings.ToUpper(s)
   220  	}
   221  }
   222  
   223  // SetEndpoint sets the endpoint associated with the icon.
   224  func (icon *LoginIcon) SetEndpoint(s string) {
   225  	icon.Endpoint = s
   226  }
   227  
   228  // GetConfig returns icon configuration.
   229  func (icon *LoginIcon) GetConfig() map[string]string {
   230  	cfg := make(map[string]string)
   231  	var entries map[string]interface{}
   232  	j, _ := json.Marshal(icon)
   233  	json.Unmarshal(j, &entries)
   234  	for k, v := range entries {
   235  		switch {
   236  		case k == "priority":
   237  		case strings.HasSuffix(k, "_enabled"):
   238  			if v.(bool) {
   239  				cfg[k] = "yes"
   240  			} else {
   241  				cfg[k] = "no"
   242  			}
   243  		default:
   244  			cfg[k] = v.(string)
   245  		}
   246  	}
   247  	return cfg
   248  }
   249  
   250  // Parse parses input and outputs login icon config.
   251  func Parse(args []string) (map[string]interface{}, error) {
   252  	section := "icon"
   253  	cfg := make(map[string]interface{})
   254  	icon := &LoginIcon{}
   255  	if len(args) < 1 {
   256  		return nil, fmt.Errorf("too few arguments")
   257  	}
   258  
   259  	var cursor, sectionCursor int
   260  	for {
   261  		if cursor >= len(args) {
   262  			break
   263  		}
   264  		switch {
   265  		case args[cursor] == "priority":
   266  			section = "priority"
   267  			sectionCursor = 0
   268  		case args[cursor] == "text":
   269  			section = "text"
   270  			sectionCursor = 0
   271  		case sectionCursor == 0 && section == "priority":
   272  			i, err := strconv.Atoi(args[cursor])
   273  			if err != nil {
   274  				return nil, err
   275  			}
   276  			icon.Priority = i
   277  		case sectionCursor == 0 && section == "icon":
   278  			icon.Text = args[0]
   279  			sectionCursor++
   280  		case sectionCursor == 1 && section == "icon":
   281  			icon.ClassName = args[cursor]
   282  			sectionCursor++
   283  		case sectionCursor == 2 && section == "icon":
   284  			icon.Color = args[cursor]
   285  			sectionCursor++
   286  		case sectionCursor == 3 && section == "icon":
   287  			icon.BackgroundColor = args[cursor]
   288  			sectionCursor++
   289  		case sectionCursor == 0 && section == "text":
   290  			icon.TextColor = args[cursor]
   291  			sectionCursor++
   292  		case sectionCursor == 1 && section == "text":
   293  			icon.TextBackgroundColor = args[cursor]
   294  			sectionCursor++
   295  		}
   296  		cursor++
   297  	}
   298  
   299  	j, _ := json.Marshal(icon)
   300  	json.Unmarshal(j, &cfg)
   301  	return cfg, nil
   302  }