Skip to content

Commit

Permalink
feat: add saml metadata force update every 24 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
hf committed Apr 6, 2023
1 parent 63bc007 commit 02cc002
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions internal/api/samlacs.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ func (a *API) samlDestroyRelayState(ctx context.Context, relayState *models.SAML
})
}

func IsMetadataStale(idpMetadata *saml.EntityDescriptor, samlProvider models.SAMLProvider) bool {
hasIDPMetadataExpired := !idpMetadata.ValidUntil.IsZero() && idpMetadata.ValidUntil.Before(time.Now())
hasCacheDurationExceeded := idpMetadata.CacheDuration != 0 && samlProvider.UpdatedAt.Add(idpMetadata.CacheDuration).Before(time.Now())
return hasIDPMetadataExpired || hasCacheDurationExceeded
func isSAMLMetadataStale(idpMetadata *saml.EntityDescriptor, samlProvider models.SAMLProvider) bool {
now := time.Now()

hasValidityExpired := !idpMetadata.ValidUntil.IsZero() && now.After(idpMetadata.ValidUntil)
hasCacheDurationExceeded := idpMetadata.CacheDuration != 0 && now.After(samlProvider.UpdatedAt.Add(idpMetadata.CacheDuration))

// if metadata XML does not publish validity or caching information, update once in 24 hours
needsForceUpdate := idpMetadata.ValidUntil.IsZero() && idpMetadata.CacheDuration == 0 && now.After(saml.Provider.UpdatedAt.Add(24*time.Hour))

return hasValidityExpired || hasCacheDurationExceeded || needsForceUpdate
}

// SAMLACS implements the main Assertion Consumer Service endpoint behavior.
Expand Down Expand Up @@ -154,7 +160,7 @@ func (a *API) SAMLACS(w http.ResponseWriter, r *http.Request) error {

logentry.Warn("SAML Metadata for identity provider will expire soon! Update its metadata_xml!")
}
} else if *ssoProvider.SAMLProvider.MetadataURL != "" && IsMetadataStale(idpMetadata, ssoProvider.SAMLProvider) {
} else if *ssoProvider.SAMLProvider.MetadataURL != "" && isSAMLMetadataStale(idpMetadata, ssoProvider.SAMLProvider) {
rawMetadata, err := fetchSAMLMetadata(ctx, *ssoProvider.SAMLProvider.MetadataURL)
if err != nil {
// Fail silently but raise warning and continue with existing metadata
Expand Down

0 comments on commit 02cc002

Please sign in to comment.