Skip to content

Commit dd3cd0e

Browse files
committed
Added the feature of setting access restriction type and email/domain whitelist blaklist
1 parent edfbaf0 commit dd3cd0e

10 files changed

Lines changed: 409 additions & 1 deletion

File tree

api/securityConfiguration.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ type ResetResponse struct {
1414
XToken string `json:"xtoken"`
1515
}
1616

17+
type EmailResponse struct {
18+
Domains []string `json:"Domains"`
19+
ListType string `json:"ListType"`
20+
RegistrationType string `json:"RegistrationType"`
21+
}
22+
23+
type RegistrationRestrictionTypeSchema struct {
24+
SelectedRestrictionType string `json:"selectedRestrictionType"`
25+
}
26+
27+
type EmailWhiteBLackListSchema struct {
28+
Domains []string `json:"Domains"`
29+
}
30+
1731
func ResetSecret() error {
1832

1933
// Restting the Secret
@@ -59,3 +73,40 @@ func ResetSecret() error {
5973
return nil
6074

6175
}
76+
77+
func GetEmailWhiteListBlackList() (*EmailResponse, error) {
78+
79+
url := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/config?"
80+
resp, err := request.Rest(http.MethodGet, url, nil, "")
81+
if err != nil {
82+
return nil, err
83+
}
84+
var resObj EmailResponse
85+
err = json.Unmarshal(resp, &resObj)
86+
if err != nil {
87+
return nil, err
88+
}
89+
return &resObj, nil
90+
91+
}
92+
93+
func AddEmailWhitelistBlacklist(restrictionType RegistrationRestrictionTypeSchema, data EmailWhiteBLackListSchema) error {
94+
typeUrl := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/type?"
95+
body, _ := json.Marshal(restrictionType)
96+
_, err := request.Rest(http.MethodPut, typeUrl, nil, string(body))
97+
98+
if err != nil {
99+
return err
100+
}
101+
102+
if restrictionType.SelectedRestrictionType != "none" {
103+
domainUrl := conf.AdminConsoleAPIDomain + "/security-configuration/restriction/config?"
104+
body, _ := json.Marshal(data)
105+
_, err := request.Rest(http.MethodPost, domainUrl, nil, string(body))
106+
107+
if err != nil {
108+
return err
109+
}
110+
}
111+
return nil
112+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package accessRestriction
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/AlecAivazis/survey/v2"
10+
"github.com/loginradius/lr-cli/api"
11+
12+
"github.com/loginradius/lr-cli/cmdutil"
13+
"github.com/loginradius/lr-cli/prompt"
14+
15+
16+
"github.com/spf13/cobra"
17+
)
18+
19+
type domain struct {
20+
Domain string `json:"domain"`
21+
}
22+
23+
func NewaccessRestrictionCmd() *cobra.Command {
24+
25+
cmd := &cobra.Command{
26+
Use: "access-restriction",
27+
Short: "Whitelist/Blacklist a domain/email",
28+
Long: `Use this command to Whitelist/Blacklist domain/email or to disable access restriction.`,
29+
Example: heredoc.Doc(`$ lr add access-restriction
30+
? Select the Restriction Type: <Type>
31+
? Enter Domain/Email: <domain>
32+
(if whitelist domain are added and you want to add blacklist domains vice/versa)
33+
? Are you Sure you want to add Domain/Email to BlackList Domains/Emails as all the whitelist Domains/Emails will be deleted ? Yes
34+
<Type> domains/emails have been updated
35+
`),
36+
RunE: func(cmd *cobra.Command, args []string) error {
37+
return addAccessRestriction()
38+
},
39+
}
40+
41+
return cmd
42+
}
43+
44+
func addAccessRestriction() error {
45+
var restrictionType = []string {"None","WhiteList", "BlackList"}
46+
var num int
47+
var shouldAdd bool
48+
err := prompt.SurveyAskOne(&survey.Select{
49+
Message: "Select the Restriction Type:",
50+
Options: restrictionType,
51+
}, &num)
52+
if err != nil {
53+
return err
54+
}
55+
var restrictType api.RegistrationRestrictionTypeSchema
56+
restrictType.SelectedRestrictionType = strings.ToLower(restrictionType[num])
57+
var AddEmail api.EmailWhiteBLackListSchema
58+
if restrictionType[num] != "None" {
59+
resp, err := api.GetEmailWhiteListBlackList()
60+
if err != nil {
61+
if err.Error() != "No records found" {
62+
return err
63+
}
64+
} else {
65+
if strings.ToLower(resp.ListType) == strings.ToLower(restrictType.SelectedRestrictionType) {
66+
AddEmail.Domains = resp.Domains
67+
}
68+
}
69+
var email string
70+
prompt.SurveyAskOne(&survey.Input{
71+
Message: "Enter Domain/Email:",
72+
}, &email, survey.WithValidator(survey.Required))
73+
if !cmdutil.AccessRestrictionDomain.MatchString(email) {
74+
return &cmdutil.FlagError{Err: errors.New("Domain/Email field is invalid")}
75+
}
76+
for _, val := range AddEmail.Domains {
77+
if val == email {
78+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email is already added")}
79+
}
80+
}
81+
if resp != nil && restrictionType[num] != resp.ListType {
82+
83+
if err := prompt.Confirm("Are you Sure you want to add Domain/Email to " + restrictionType[num] + " Domains as all the " + resp.ListType + " Domains/Emails will be deleted ?",
84+
&shouldAdd); err != nil {
85+
return err
86+
}
87+
} else {
88+
shouldAdd = true
89+
}
90+
91+
AddEmail.Domains = append(AddEmail.Domains, email)
92+
93+
} else {
94+
if err := prompt.Confirm("Are you Sure you want to disable access restrictions as all the WhiteList/Blacklist Domains/Emails will be deleted ?",
95+
&shouldAdd); err != nil {
96+
return err
97+
}
98+
}
99+
100+
if shouldAdd {
101+
err = api.AddEmailWhitelistBlacklist(restrictType, AddEmail);
102+
if err != nil {
103+
return err
104+
}
105+
if restrictType.SelectedRestrictionType == "none" {
106+
fmt.Println("Access restrictions have been successfully disabled " )
107+
} else {
108+
fmt.Println(restrictionType[num] + " domains/emails have been updated" )
109+
}
110+
}
111+
112+
return nil
113+
114+
}

cmd/add/add.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/loginradius/lr-cli/cmd/add/hooks"
1010
"github.com/loginradius/lr-cli/cmd/add/social"
11+
"github.com/loginradius/lr-cli/cmd/add/accessRestriction"
1112

1213
"github.com/spf13/cobra"
1314
)
@@ -44,5 +45,8 @@ func NewaddCmd() *cobra.Command {
4445
customFieldsCmd := customField.NewAddCFCmd()
4546
cmd.AddCommand(customFieldsCmd)
4647

48+
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
49+
cmd.AddCommand(accessRestrictionCmd)
50+
4751
return cmd
4852
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package accessRestriction
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/MakeNowJust/heredoc"
9+
"github.com/loginradius/lr-cli/api"
10+
"github.com/loginradius/lr-cli/cmdutil"
11+
12+
"github.com/spf13/cobra"
13+
)
14+
15+
16+
type domain struct {
17+
BlacklistDomain string `json:"blacklistdomain"`
18+
WhitelistDomain string `json:"whitelistdomain"`
19+
}
20+
21+
22+
func NewaccessRestrictionCmd() *cobra.Command {
23+
opts := &domain{}
24+
25+
cmd := &cobra.Command{
26+
Use: "access-restriction",
27+
Short: "Deletes whitelisted/blacklisted domain/emails",
28+
Long: `Use this command to remove the whitelisted/blacklisted domain/emails.`,
29+
Example: heredoc.Doc(`$ lr delete access-restriction --blacklist-domain <domain>
30+
<Type> domains/emails have been updated"
31+
`),
32+
RunE: func(cmd *cobra.Command, args []string) error {
33+
if opts.BlacklistDomain == "" && opts.WhitelistDomain == "" {
34+
return &cmdutil.FlagError{Err: errors.New("`domain` is required argument")}
35+
}
36+
37+
resp, err := api.GetEmailWhiteListBlackList()
38+
if err != nil {
39+
return err
40+
}
41+
if (resp.ListType == "WhiteList" && opts.BlacklistDomain != "") || (resp.ListType == "BlackList" && opts.WhitelistDomain != "") {
42+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email Not Found. As " + resp.ListType + " Restriction Type is selected. You can change it via `lr add access-restriction`" )}
43+
44+
}
45+
var domain string
46+
if opts.BlacklistDomain != "" {
47+
domain = opts.BlacklistDomain
48+
} else if opts.WhitelistDomain != "" {
49+
domain = opts.WhitelistDomain
50+
}
51+
52+
_, found := cmdutil.Find(resp.Domains, domain)
53+
if !found {
54+
return &cmdutil.FlagError{Err: errors.New("Entered Domain/Email not found")}
55+
}
56+
57+
var newDomains []string
58+
newDomains = resp.Domains
59+
for index, url := range resp.Domains {
60+
if url == domain {
61+
newDomains = append(resp.Domains[:index], resp.Domains[index+1:]...)
62+
break
63+
}
64+
}
65+
delete(resp.ListType, newDomains)
66+
return nil
67+
68+
},
69+
}
70+
71+
fl := cmd.Flags()
72+
fl.StringVarP(&opts.BlacklistDomain, "blacklist-domain", "b", "", "Enter Blacklist Domain/Email Value you want to delete")
73+
fl.StringVarP(&opts.WhitelistDomain, "whitelist-domain", "w", "", "Enter Whitelist Domain/Email Value you want to delete")
74+
return cmd
75+
}
76+
77+
func delete(listType string, domain []string) error {
78+
var restrictType api.RegistrationRestrictionTypeSchema
79+
restrictType.SelectedRestrictionType = strings.ToLower(listType)
80+
var AddEmail api.EmailWhiteBLackListSchema
81+
AddEmail.Domains = domain
82+
err := api.AddEmailWhitelistBlacklist(restrictType, AddEmail);
83+
if err != nil {
84+
return err
85+
}
86+
fmt.Println(listType + " domains/emails have been updated" )
87+
return nil
88+
}

cmd/delete/delete.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/loginradius/lr-cli/cmd/delete/sott"
99

1010
"github.com/loginradius/lr-cli/cmd/delete/social"
11+
"github.com/loginradius/lr-cli/cmd/delete/accessRestriction"
1112

1213
"github.com/spf13/cobra"
1314
)
@@ -44,5 +45,8 @@ func NewdeleteCmd() *cobra.Command {
4445
customFieldsCmd := customField.NewDeleteCFCmd()
4546
cmd.AddCommand(customFieldsCmd)
4647

48+
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
49+
cmd.AddCommand(accessRestrictionCmd)
50+
4751
return cmd
4852
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package accessRestriction
2+
3+
import (
4+
"fmt"
5+
// "strings"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/loginradius/lr-cli/api"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func NewaccessRestrictionCmd() *cobra.Command {
13+
14+
cmd := &cobra.Command{
15+
Use: "access-restriction",
16+
Short: "Gets whitelisted/blacklisted domains/emails",
17+
Long: `Use this command to get the list of the whitelist/blacklist domains/emails.`,
18+
Example: heredoc.Doc(`$ lr get access-restriction
19+
WhiteList/Blacklist Domains/Emails
20+
1. http://localhost
21+
...
22+
`),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
25+
resp, err := api.GetEmailWhiteListBlackList()
26+
if err != nil {
27+
return err
28+
}
29+
fmt.Println(resp.ListType + " Domains/Emails")
30+
for i := 0; i < len(resp.Domains); i++ {
31+
fmt.Print(fmt.Sprint(i+1) + ". ")
32+
fmt.Println(resp.Domains[i])
33+
}
34+
return nil
35+
36+
},
37+
}
38+
39+
return cmd
40+
}

cmd/get/get.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/loginradius/lr-cli/cmd/get/serverInfo"
1616
"github.com/loginradius/lr-cli/cmd/get/social"
1717
"github.com/loginradius/lr-cli/cmd/get/theme"
18+
"github.com/loginradius/lr-cli/cmd/get/accessRestriction"
1819

1920
"github.com/spf13/cobra"
2021
)
@@ -65,5 +66,9 @@ func NewGetCmd() *cobra.Command {
6566

6667
schemaCmd := schema.NewschemaCmd()
6768
cmd.AddCommand(schemaCmd)
69+
70+
accessRestrictionCmd := accessRestriction.NewaccessRestrictionCmd()
71+
cmd.AddCommand(accessRestrictionCmd)
72+
6873
return cmd
6974
}

0 commit comments

Comments
 (0)