|
| 1 | +package fastac_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/abichinger/fastac" |
| 8 | + "github.com/abichinger/fastac/model" |
| 9 | + "github.com/abichinger/fastac/rbac" |
| 10 | + "github.com/abichinger/fastac/util" |
| 11 | +) |
| 12 | + |
| 13 | +//the model uses the built-in MatchingFunc pathMatch |
| 14 | +var example_matcher_model = ` |
| 15 | +[request_definition] |
| 16 | +r = sub, obj, act |
| 17 | +
|
| 18 | +[policy_definition] |
| 19 | +p = sub, obj, act |
| 20 | +
|
| 21 | +[role_definition] |
| 22 | +g = _, _ |
| 23 | +
|
| 24 | +[policy_effect] |
| 25 | +e = some(where (p.eft == allow)) |
| 26 | +
|
| 27 | +[matchers] |
| 28 | +m = g(r.sub, p.sub) && pathMatch(r.obj, p.obj) && r.act == p.act` |
| 29 | + |
| 30 | +var example_matcher_policy = [][]string{ |
| 31 | + {"p", "role:user", "/user/:uid/entry/:eid", "GET"}, |
| 32 | + {"p", "user:alice", "/user/alice/*", "POST"}, |
| 33 | + {"p", "role:admin", "/user/:uid/entry/:eid", "DELETE"}, |
| 34 | + {"g", "reg:user:.*", "role:user"}, |
| 35 | + {"g", "user:alice", "role:admin"}, |
| 36 | +} |
| 37 | + |
| 38 | +func printReq(e *fastac.Enforcer, params ...interface{}) { |
| 39 | + b, _ := e.Enforce(params...) |
| 40 | + var rule []string |
| 41 | + for _, param := range params { |
| 42 | + rule = append(rule, param.(string)) |
| 43 | + } |
| 44 | + if b { |
| 45 | + fmt.Printf("%s => allow\n", strings.Join(rule, ", ")) |
| 46 | + } else { |
| 47 | + fmt.Printf("%s => deny\n", strings.Join(rule, ", ")) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// ExampleMatchers shows the usage of util.MatchingFunc and util.IMatcher |
| 52 | +func Example_matchers() { |
| 53 | + |
| 54 | + //create enforcer and add rules |
| 55 | + m := model.NewModel() |
| 56 | + _ = m.LoadModelFromText(example_matcher_model) |
| 57 | + e, _ := fastac.NewEnforcer(m, nil) |
| 58 | + _ = e.AddRules(example_matcher_policy) |
| 59 | + |
| 60 | + //get the default rolemanager |
| 61 | + rm, _ := e.GetModel().GetRoleManager("g") |
| 62 | + |
| 63 | + // set a role matcher. |
| 64 | + // create a PrefixMatcher. PrefixMatcher implements the interface util.IMatcher |
| 65 | + // each regex pattern needs to be marked with the prefix "reg:" |
| 66 | + roleMatcher := util.NewPrefixMatcher("reg:", util.RegexMatch) |
| 67 | + rm.(rbac.IDefaultRoleManager).SetMatcher(roleMatcher) |
| 68 | + |
| 69 | + printReq(e, "user:alice", "/user/joe/entry/1", "GET") //allow, because user:alice has role:user |
| 70 | + printReq(e, "user:alice", "/user/alice/entry/2", "POST") |
| 71 | + printReq(e, "user:alice", "/user/bob/entry/3", "POST") |
| 72 | + printReq(e, "user:alice", "/user/bob/entry/3", "DELETE") |
| 73 | + printReq(e, "user:bob", "/user/alice/entry/2", "DELETE") |
| 74 | + |
| 75 | + // Output: user:alice, /user/joe/entry/1, GET => allow |
| 76 | + // user:alice, /user/alice/entry/2, POST => allow |
| 77 | + // user:alice, /user/bob/entry/3, POST => deny |
| 78 | + // user:alice, /user/bob/entry/3, DELETE => allow |
| 79 | + // user:bob, /user/alice/entry/2, DELETE => deny |
| 80 | +} |
0 commit comments