2019-04-25 04:52:08 +07:00
|
|
|
package authorization
|
|
|
|
|
|
|
|
import (
|
2021-08-11 08:04:35 +07:00
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
"github.com/authelia/authelia/v4/internal/logging"
|
2019-04-25 04:52:08 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Authorizer the component in charge of checking whether a user can access a given resource.
|
|
|
|
type Authorizer struct {
|
2021-03-05 11:18:31 +07:00
|
|
|
defaultPolicy Level
|
|
|
|
rules []*AccessControlRule
|
2021-06-18 08:38:01 +07:00
|
|
|
configuration *schema.Configuration
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAuthorizer create an instance of authorizer with a given access control configuration.
|
2021-06-18 08:38:01 +07:00
|
|
|
func NewAuthorizer(configuration *schema.Configuration) *Authorizer {
|
2019-04-25 04:52:08 +07:00
|
|
|
return &Authorizer{
|
2021-06-18 08:38:01 +07:00
|
|
|
defaultPolicy: PolicyToLevel(configuration.AccessControl.DefaultPolicy),
|
|
|
|
rules: NewAccessControlRules(configuration.AccessControl),
|
|
|
|
configuration: configuration,
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:31:09 +07:00
|
|
|
// IsSecondFactorEnabled return true if at least one policy is set to second factor.
|
2021-06-18 08:38:01 +07:00
|
|
|
func (p Authorizer) IsSecondFactorEnabled() bool {
|
2021-03-05 11:18:31 +07:00
|
|
|
if p.defaultPolicy == TwoFactor {
|
2020-03-06 07:31:09 +07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
for _, rule := range p.rules {
|
|
|
|
if rule.Policy == TwoFactor {
|
2020-03-06 07:31:09 +07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 08:38:01 +07:00
|
|
|
if p.configuration.IdentityProviders.OIDC != nil {
|
|
|
|
for _, client := range p.configuration.IdentityProviders.OIDC.Clients {
|
|
|
|
if client.Policy == twoFactor {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 07:31:09 +07:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-25 04:52:08 +07:00
|
|
|
// GetRequiredLevel retrieve the required level of authorization to access the object.
|
2021-04-14 17:53:23 +07:00
|
|
|
func (p Authorizer) GetRequiredLevel(subject Subject, object Object) Level {
|
2021-01-17 06:23:35 +07:00
|
|
|
logger := logging.Logger()
|
2021-04-14 17:53:23 +07:00
|
|
|
|
|
|
|
logger.Debugf("Check authorization of subject %s and object %s (method %s).",
|
|
|
|
subject.String(), object.String(), object.Method)
|
2019-04-25 04:52:08 +07:00
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
for _, rule := range p.rules {
|
|
|
|
if rule.IsMatch(subject, object) {
|
2021-04-14 17:53:23 +07:00
|
|
|
logger.Tracef(traceFmtACLHitMiss, "HIT", rule.Position, subject.String(), object.String(), object.Method)
|
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
return rule.Policy
|
|
|
|
}
|
2021-04-14 17:53:23 +07:00
|
|
|
|
|
|
|
logger.Tracef(traceFmtACLHitMiss, "MISS", rule.Position, subject.String(), object.String(), object.Method)
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|
2020-05-06 02:35:32 +07:00
|
|
|
|
2021-04-14 17:53:23 +07:00
|
|
|
logger.Debugf("No matching rule for subject %s and url %s... Applying default policy.",
|
|
|
|
subject.String(), object.String())
|
2020-02-19 05:15:09 +07:00
|
|
|
|
2021-03-05 11:18:31 +07:00
|
|
|
return p.defaultPolicy
|
2019-04-25 04:52:08 +07:00
|
|
|
}
|