-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching support to IDPMetadata() (#102)
* Add caching support to IDPMetadata() Caching the metadata document will avoid an additional round-trip to the IDP for every connection. The Metadata for the OASIS Security Assertion Markup Language says regarding caching: 4.3 Post-Processing of Metadata The following sections describe the post-processing of metadata. 4.3.1 Metadata Instance Caching [E94] Document caching MUST be based on the duration indicated by the cacheDuration attribute of the subject element(s). If metadata elements have parent elements which contain caching policies, the parent element takes precedence. To properly process the cacheDuration attribute, consumers must retain the date and time when an instance was obtained. Note that cache expiration does not imply a lack of validity in the absence of a validUntil attribute or other information; failure to update a cached instance (e.g., due to network failure) need not render metadata invalid, although implementations may offer such controls to deployers. When a document or element has expired, the consumer MUST retrieve a fresh copy, which may require a refresh of the document location(s). Consumers SHOULD process document cache processing according to [RFC2616] Section 13, and MAY request the Last-Modified date and time from the HTTP server. Publishers SHOULD ensure acceptable cache processing as described in [RFC2616] (Section 10.3.5 304 Not Modified). 4.3.2 [E94] Metadata Instance Validity Metadata MUST be considered invalid upon reaching the time specified in a validUntil attribute of the subject element(s). The effective expiration may be adjusted downward by parent element(s) with earlier expirations. Invalid metadata MUST NOT be used. This contrasts with "stale" metadata that may be beyond its optimum cache duration but is not explicitly invalid. Such metadata remains valid and MAY be used at the discretion of the implementation. With this change the cached metadata is used until it expires. This behavior can be disabled using WithCache(). Using a stale document when refreshing it fails is disabled by default and users can opt-in using WithStale(). * Address code review comments * Run go mod tidy * Run go mod tidy * Update saml/sp_test.go Co-authored-by: Jim <jlambert@hashicorp.com> --------- Co-authored-by: Jim <jlambert@hashicorp.com>
- Loading branch information
1 parent
6f5c72b
commit b0ed5aa
Showing
8 changed files
with
362 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package metadata | ||
|
||
import ( | ||
"time" | ||
|
||
crewjamSaml "github.com/crewjam/saml" | ||
) | ||
|
||
// Duration is a time.Duration that uses the xsd:duration format for text | ||
// marshalling and unmarshalling. | ||
type Duration time.Duration | ||
|
||
// MarshalText implements the encoding.TextMarshaler interface. | ||
func (d Duration) MarshalText() ([]byte, error) { | ||
return crewjamSaml.Duration(d).MarshalText() | ||
} | ||
|
||
// UnmarshalText implements the encoding.TextUnmarshaler interface. | ||
func (d *Duration) UnmarshalText(text []byte) error { | ||
cp := (*crewjamSaml.Duration)(d) | ||
return cp.UnmarshalText(text) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package metadata | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var durationMarshalTests = []struct { | ||
in time.Duration | ||
expected []byte | ||
}{ | ||
{0, nil}, | ||
{time.Hour, []byte("PT1H")}, | ||
{-time.Hour, []byte("-PT1H")}, | ||
} | ||
|
||
func TestDuration(t *testing.T) { | ||
for i, testCase := range durationMarshalTests { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
actual, err := Duration(testCase.in).MarshalText() | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.expected, actual) | ||
}) | ||
} | ||
} | ||
|
||
var durationUnmarshalTests = []struct { | ||
in []byte | ||
expected time.Duration | ||
err error | ||
}{ | ||
{nil, 0, nil}, | ||
{[]byte("-PT1H"), -time.Hour, nil}, | ||
{[]byte("P1D"), 24 * time.Hour, nil}, | ||
{[]byte("P1M"), 720 * time.Hour, nil}, | ||
{[]byte("PT1.S"), 0, errors.New("invalid duration (PT1.S)")}, | ||
} | ||
|
||
func TestDurationUnmarshal(t *testing.T) { | ||
for i, testCase := range durationUnmarshalTests { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
var actual Duration | ||
err := actual.UnmarshalText(testCase.in) | ||
if testCase.err == nil { | ||
require.NoError(t, err) | ||
} else { | ||
require.ErrorContains(t, err, testCase.err.Error()) | ||
} | ||
require.Equal(t, Duration(testCase.expected), actual) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.