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