Skip to content

Commit

Permalink
fix: add custom time format option
Browse files Browse the repository at this point in the history
  • Loading branch information
stebenz committed Mar 7, 2023
1 parent 71ad690 commit bb3761f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pkg/provider/identityprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (p *IdentityProvider) GetMetadata(ctx context.Context) (*md.IDPSSODescripto
return nil, nil, err
}

metadata, aaMetadata := p.conf.getMetadata(ctx, p.GetEntityID(ctx), cert)
metadata, aaMetadata := p.conf.getMetadata(ctx, p.GetEntityID(ctx), cert, p.timeFormat)
return metadata, aaMetadata, nil
}

Expand Down
1 change: 1 addition & 0 deletions pkg/provider/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (p *IdentityProvider) logoutHandleFunc(w http.ResponseWriter, r *http.Reque
checkIfRequestTimeIsStillValid(
func() string { return logoutRequest.IssueInstant },
func() string { return logoutRequest.NotOnOrAfter },
p.timeFormat,
),
func() {
response.sendBackLogoutResponse(w, response.makeDeniedLogoutResponse(fmt.Errorf("failed to validate request: %w", err).Error(), p.timeFormat))
Expand Down
3 changes: 2 additions & 1 deletion pkg/provider/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func (p *IdentityProviderConfig) getMetadata(
ctx context.Context,
entityID string,
idpCertData []byte,
timeFormat string,
) (*md.IDPSSODescriptorType, *md.AttributeAuthorityDescriptorType) {
endpoints := endpointConfigToEndpoints(p.Endpoints)

Expand Down Expand Up @@ -80,7 +81,7 @@ func (p *IdentityProviderConfig) getMetadata(
}
validUntil := ""
if p.MetadataIDPConfig.ValidUntil != 0 {
validUntil = time.Now().Add(p.MetadataIDPConfig.ValidUntil).UTC().Format(defaultTimeLayout)
validUntil = time.Now().Add(p.MetadataIDPConfig.ValidUntil).UTC().Format(timeFormat)
}
cacheDuration := ""
if p.MetadataIDPConfig.CacheDuration != "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/provider/sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ func checkRequestRequiredContent(
if err := checkIfRequestTimeIsStillValid(
func() string { return authNRequest.Conditions.NotBefore },
func() string { return authNRequest.Conditions.NotOnOrAfter },
DefaultTimeFormat,
)(); err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/provider/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ import (
"time"
)

const defaultTimeLayout = "2006-01-02T15:04:05.999999Z"

func checkIfRequestTimeIsStillValid(notBefore func() string, notOnOrAfter func() string) func() error {
func checkIfRequestTimeIsStillValid(notBefore func() string, notOnOrAfter func() string, timeFormat string) func() error {
return func() error {
now := time.Now().UTC()
if notBefore() != "" {
t, err := time.Parse(defaultTimeLayout, notBefore())
t, err := time.Parse(timeFormat, notBefore())
if err != nil {
return fmt.Errorf("failed to parse NotBefore: %w", err)
}
Expand All @@ -21,7 +19,7 @@ func checkIfRequestTimeIsStillValid(notBefore func() string, notOnOrAfter func()
}

if notOnOrAfter() != "" {
t, err := time.Parse(defaultTimeLayout, notOnOrAfter())
t, err := time.Parse(timeFormat, notOnOrAfter())
if err != nil {
return fmt.Errorf("failed to parse NotOnOrAfter: %w", err)
}
Expand Down
50 changes: 35 additions & 15 deletions pkg/provider/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"time"
)

const (
otherTimeFormat = "2006-01-02T15:04:05.999Z"
)

func TestTime_checkIfRequestTimeIsStillValid(t *testing.T) {
type args struct {
notBefore string
Expand All @@ -20,40 +24,56 @@ func TestTime_checkIfRequestTimeIsStillValid(t *testing.T) {
{
"check ok 1",
args{
notBefore: now.Add(-1 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(1 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(-1 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: now.Add(1 * time.Minute).Format(DefaultTimeFormat),
},
false,
},
{
"check ok 2",
args{
notBefore: now.Add(-1 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(5 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(-1 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: now.Add(5 * time.Minute).Format(DefaultTimeFormat),
},
false,
},
{
"check ok 3",
args{
notBefore: now.Add(-5 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(5 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(-5 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: now.Add(5 * time.Minute).Format(DefaultTimeFormat),
},
false,
},
{
"check ok otherformat",
args{
notBefore: now.Add(-5 * time.Minute).Format(otherTimeFormat),
notOnOrAfter: now.Add(5 * time.Minute).Format(otherTimeFormat),
},
false,
},
{
"check not ok 1",
args{
notBefore: now.Add(1 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(5 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(1 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: now.Add(5 * time.Minute).Format(DefaultTimeFormat),
},
true,
},
{
"check not ok 2",
args{
notBefore: now.Add(-5 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(-1 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(-5 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: now.Add(-1 * time.Minute).Format(DefaultTimeFormat),
},
true,
},
{
"check not ok otherFormat",
args{
notBefore: now.Add(-5 * time.Minute).Format(otherTimeFormat),
notOnOrAfter: now.Add(-1 * time.Minute).Format(otherTimeFormat),
},
true,
},
Expand All @@ -69,30 +89,30 @@ func TestTime_checkIfRequestTimeIsStillValid(t *testing.T) {
"check ok only notOnOrAfter",
args{
notBefore: "",
notOnOrAfter: now.Add(1 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(1 * time.Minute).Format(DefaultTimeFormat),
},
false,
},
{
"check not ok only notOnOrAfter",
args{
notBefore: "",
notOnOrAfter: now.Add(-1 * time.Minute).Format(defaultTimeLayout),
notOnOrAfter: now.Add(-1 * time.Minute).Format(DefaultTimeFormat),
},
true,
},
{
"check not ok only notBefore",
args{
notBefore: now.Add(1 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(1 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: "",
},
true,
},
{
"check ok only notBefore",
args{
notBefore: now.Add(-1 * time.Minute).Format(defaultTimeLayout),
notBefore: now.Add(-1 * time.Minute).Format(DefaultTimeFormat),
notOnOrAfter: "",
},
false,
Expand Down Expand Up @@ -124,7 +144,7 @@ func TestTime_checkIfRequestTimeIsStillValid(t *testing.T) {
return tt.args.notOnOrAfter
}

errF := checkIfRequestTimeIsStillValid(notBeforeF, notOnOrAfterF)
errF := checkIfRequestTimeIsStillValid(notBeforeF, notOnOrAfterF, DefaultTimeFormat)
err := errF()
if (err != nil) != tt.res {
t.Errorf("ParseCertificates() got = %v, want %v", err != nil, tt.res)
Expand Down

0 comments on commit bb3761f

Please sign in to comment.