Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added setting: Strict validation of accesscodes #153

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions pkg/authserver/authserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,20 @@ func (a AuthServer) AddAccessCodeFunc(w http.ResponseWriter, r *http.Request) {
return
}

// Validate that the AccessCode exists if strict setting enabled
if set := settingclient.GetSetting(settingclient.AccessCodeValidation); set == nil {
util.ReturnHTTPMessage(w, r, 500, "internalerror", "error performing registration")
return
} else {
if set.(bool) {
err := a.ValidateAccessCodeFunc(accessCode)
if err != nil {
util.ReturnHTTPMessage(w, r, 400, "badrequest", "AccessCode is invalid.")
return
}
}
}

err = a.AddAccessCode(user.Name, accessCode)

if err != nil {
Expand All @@ -312,6 +326,16 @@ func (a AuthServer) AddAccessCodeFunc(w http.ResponseWriter, r *http.Request) {
glog.V(2).Infof("added accesscode %s to user %s", accessCode, user.Spec.Email)
}

func (a AuthServer) ValidateAccessCodeFunc(accessCode string) error {
_, err := a.hfClientSet.HobbyfarmV1().AccessCodes(util.GetReleaseNamespace()).Get(a.ctx, accessCode, metav1.GetOptions{})
if err != nil {
// If AccessCode does not exist check if this might be an OTAC
_, err := a.hfClientSet.HobbyfarmV1().OneTimeAccessCodes(util.GetReleaseNamespace()).Get(a.ctx, accessCode, metav1.GetOptions{})
return err
}
return nil
}

func (a AuthServer) RemoveAccessCodeFunc(w http.ResponseWriter, r *http.Request) {
user, err := a.auth.AuthN(w, r)
if err != nil {
Expand Down Expand Up @@ -365,6 +389,8 @@ func (a AuthServer) AddAccessCode(userId string, accessCode string) error {
// when we are here the user had the otac added previously
}



retryErr := retry.RetryOnConflict(retry.DefaultRetry, func() error {
user, err := a.hfClientSet.HobbyfarmV2().Users(util.GetReleaseNamespace()).Get(a.ctx, userId, metav1.GetOptions{})

Expand Down Expand Up @@ -546,6 +572,20 @@ func (a AuthServer) RegisterWithAccessCodeFunc(w http.ResponseWriter, r *http.Re
return
}

// Validate that the AccessCode exists if strict setting enabled
if set := settingclient.GetSetting(settingclient.AccessCodeValidation); set == nil {
util.ReturnHTTPMessage(w, r, 500, "internalerror", "error performing registration")
return
} else {
if set.(bool) {
err := a.ValidateAccessCodeFunc(accessCode)
if err != nil {
util.ReturnHTTPMessage(w, r, 400, "badrequest", "AccessCode is invalid.")
return
}
}
}

userId, err := a.NewUser(email, password)

if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/preinstall/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,20 @@ func settings() []*v1.Setting {
DisplayName: "ScheduledEvent retention time (h)",
},
},
{
ObjectMeta: v12.ObjectMeta{
Name: string(settingclient.AccessCodeValidation),
Namespace: util.GetReleaseNamespace(),
Labels: map[string]string{
labels.SettingScope: "gargantua",
},
},
Value: "false",
Property: property.Property{
DataType: property.DataTypeBoolean,
ValueType: property.ValueTypeScalar,
DisplayName: "Strict AccessCode validation",
},
},
}
}
1 change: 1 addition & 0 deletions pkg/settingclient/settingclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
SettingRegistrationPrivacyPolicyRequired SettingName = "registration-privacy-policy-required"
SettingRegistrationPrivacyPolicyLink SettingName = "registration-privacy-policy-link"
SettingRegistrationPrivacyPolicyLinkName SettingName = "registration-privacy-policy-linkname"
AccessCodeValidation SettingName = "accesscode-validation"
SettingAdminUIMOTD SettingName = "motd-admin-ui"
SettingUIMOTD SettingName = "motd-ui"
ScheduledEventRetentionTime SettingName = "scheduledevent-retention-time"
Expand Down