mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-22 13:37:19 +02:00
42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package criteria
|
|
|
|
import (
|
|
"github.com/open-policy-agent/opa/ast"
|
|
|
|
"github.com/pomerium/pomerium/pkg/policy/generator"
|
|
"github.com/pomerium/pomerium/pkg/policy/parser"
|
|
"github.com/pomerium/pomerium/pkg/policy/rules"
|
|
)
|
|
|
|
var authenticatedUserBody = ast.Body{
|
|
ast.MustParseExpr(`session := get_session(input.session.id)`),
|
|
ast.MustParseExpr(`session.user_id != null`),
|
|
ast.MustParseExpr(`session.user_id != ""`),
|
|
}
|
|
|
|
type authenticatedUserCriterion struct {
|
|
g *Generator
|
|
}
|
|
|
|
func (authenticatedUserCriterion) DataType() CriterionDataType {
|
|
return generator.CriterionDataTypeUnused
|
|
}
|
|
|
|
func (authenticatedUserCriterion) Name() string {
|
|
return "authenticated_user"
|
|
}
|
|
|
|
func (c authenticatedUserCriterion) GenerateRule(_ string, _ parser.Value) (*ast.Rule, []*ast.Rule, error) {
|
|
rule := c.g.NewRule("authenticated_user")
|
|
rule.Body = authenticatedUserBody
|
|
return rule, []*ast.Rule{rules.GetSession()}, nil
|
|
}
|
|
|
|
// AuthenticatedUser returns a Criterion which returns true if the current user is logged in.
|
|
func AuthenticatedUser(generator *Generator) Criterion {
|
|
return authenticatedUserCriterion{g: generator}
|
|
}
|
|
|
|
func init() {
|
|
Register(AuthenticatedUser)
|
|
}
|