Skip to content

Commit

Permalink
Add Setting for User Token Expiration (hobbyfarm#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
jggoebel authored and ebauman committed Mar 28, 2024
1 parent 247a9ff commit 6b5e9b8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
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)",
},
},
}
}

0 comments on commit 6b5e9b8

Please sign in to comment.