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

Add Setting for User Token Expiration so the user gets logged out after the configured time #178

Merged
merged 2 commits into from
Feb 20, 2024
Merged
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
4 changes: 3 additions & 1 deletion v3/pkg/setting/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package setting
import (
"encoding/json"
"fmt"
"github.com/hobbyfarm/gargantua/v3/pkg/property"
"strconv"
"strings"

"github.com/hobbyfarm/gargantua/v3/pkg/property"

settingProto "github.com/hobbyfarm/gargantua/v3/protos/setting"
)

Expand All @@ -24,6 +25,7 @@ const (
ImprintLink SettingName = "imprint-link"
ImprintLinkName SettingName = "imprint-linkname"
AboutModalButtons SettingName = "aboutmodal-buttons"
UserTokenExpiration SettingName = "user-token-expiration"
)

var DataTypeMappingToProto = map[property.DataType]settingProto.DataType{
Expand Down
20 changes: 16 additions & 4 deletions v3/services/authnsvc/internal/authnservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (a AuthServer) LoginFunc(w http.ResponseWriter, r *http.Request) {
return
}

token, err := GenerateJWT(user)
token, err := a.GenerateJWT(user)

if err != nil {
glog.Error(err)
Expand All @@ -506,12 +506,24 @@ func (a AuthServer) LoginFunc(w http.ResponseWriter, r *http.Request) {
util.ReturnHTTPMessage(w, r, 200, "authorized", token)
}

func GenerateJWT(user *userProto.User) (string, error) {
func (a AuthServer) GenerateJWT(user *userProto.User) (string, error) {
// Get Expiration Date Setting
setting, err := a.settingClient.GetSettingValue(context.Background(), &settingProto.Id{Name: string(settingUtil.UserTokenExpiration)})
if err != nil {
return "", err
}

tokenExpiration := time.Duration(24)
if s, ok := setting.GetValue().(*settingProto.SettingValue_Int64Value); err != nil || !ok || setting == nil {
return "", fmt.Errorf("error retreiving retention Time setting")
} else {
tokenExpiration = time.Duration(s.Int64Value)
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"email": user.GetEmail(),
"nbf": time.Now().Unix(), // not valid before now
"exp": time.Now().Add(time.Hour * 24).Unix(), // expire in 24 hours
"nbf": time.Now().Unix(), // not valid before now
"exp": time.Now().Add(time.Hour * tokenExpiration).Unix(), // expire after [tokenExpiration] hours
})

// Sign and get the complete encoded token as a string using the secret
Expand Down
13 changes: 13 additions & 0 deletions v3/services/settingsvc/internal/preinstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,18 @@ func predefinedSettings() []*settingProto.CreateSettingRequest {
DisplayName: "Strict AccessCode Validation",
},
},
{
Name: string(settingUtil.UserTokenExpiration),
Namespace: util.GetReleaseNamespace(),
Labels: map[string]string{
labels.SettingScope: "gargantua",
},
Value: "24",
Property: &settingProto.Property{
DataType: settingProto.DataType_DATA_TYPE_INTEGER,
ValueType: settingProto.ValueType_VALUE_TYPE_SCALAR,
DisplayName: "User Token Expiration (hours)",
},
},
}
}
Loading