verifier.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package perm
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "github.com/iancoleman/strcase"
  7. "github.com/ory/ladon"
  8. )
  9. var Verbose = false
  10. type verReq struct {
  11. subjects []string
  12. objs []interface{}
  13. r *http.Request
  14. req *ladon.Request
  15. resourcesParts []string
  16. }
  17. type Verifier struct {
  18. builder *Builder
  19. module string
  20. vr *verReq
  21. }
  22. func NewVerifier(module string, b *Builder) (r *Verifier) {
  23. r = &Verifier{
  24. module: module,
  25. }
  26. if b == nil {
  27. return r
  28. }
  29. r.builder = b
  30. return
  31. }
  32. func (b *Verifier) Spawn() (r *Verifier) {
  33. if b.builder == nil {
  34. return b
  35. }
  36. r = &Verifier{
  37. module: b.module,
  38. builder: b.builder,
  39. }
  40. resourceParts := []string{b.module}
  41. if b.vr != nil {
  42. resourceParts = b.vr.resourcesParts
  43. }
  44. r.vr = &verReq{
  45. resourcesParts: append([]string{}, resourceParts...),
  46. req: &ladon.Request{},
  47. }
  48. return
  49. }
  50. func (b *Verifier) Do(v string) (r *Verifier) {
  51. if b.builder == nil {
  52. return b
  53. }
  54. r = b.Spawn()
  55. r.vr.req.Action = v
  56. return
  57. }
  58. // SnakeDo convert string to snake form.
  59. // e.g. "SnakeDo" -> "snake_do"
  60. func (b *Verifier) SnakeDo(actions ...string) (r *Verifier) {
  61. var fixed = []string{b.module}
  62. for _, a := range actions {
  63. fixed = append(fixed, strcase.ToSnake(a))
  64. }
  65. return b.Do(strings.Join(fixed, ":"))
  66. }
  67. func (b *Verifier) On(vs ...string) (r *Verifier) {
  68. if b.builder == nil {
  69. return b
  70. }
  71. b.vr.resourcesParts = append(b.vr.resourcesParts, vs...)
  72. return b
  73. }
  74. func (b *Verifier) SnakeOn(vs ...string) (r *Verifier) {
  75. if b.builder == nil {
  76. return b
  77. }
  78. var fixed []string
  79. for _, v := range vs {
  80. if v == "" {
  81. continue
  82. }
  83. fixed = append(fixed, strcase.ToSnake(v))
  84. }
  85. b.On(fixed...)
  86. return b
  87. }
  88. func (b *Verifier) ObjectOn(v interface{}) (r *Verifier) {
  89. if b.builder == nil {
  90. return b
  91. }
  92. b.vr.objs = append(b.vr.objs, v)
  93. b.vr.resourcesParts = append(b.vr.resourcesParts, ToPermissionRN(v)...)
  94. return b
  95. }
  96. func (b *Verifier) RemoveOn(length int) (r *Verifier) {
  97. if b.builder == nil {
  98. return b
  99. }
  100. if len(b.vr.resourcesParts) >= length {
  101. b.vr.resourcesParts = b.vr.resourcesParts[:len(b.vr.resourcesParts)-length]
  102. }
  103. return b
  104. }
  105. func (b *Verifier) WithReq(v *http.Request) (r *Verifier) {
  106. if b.builder == nil {
  107. return b
  108. }
  109. b.vr.r = v
  110. return b
  111. }
  112. func (b *Verifier) From(v string) (r *Verifier) {
  113. if b.builder == nil {
  114. return b
  115. }
  116. b.vr.subjects = append(b.vr.subjects, v)
  117. return b
  118. }
  119. func (b *Verifier) Given(v ladon.Context) (r *Verifier) {
  120. if b.builder == nil {
  121. return b
  122. }
  123. b.vr.req.Context = v
  124. return b
  125. }
  126. func (b *Verifier) IsAllowed() error {
  127. if b.builder == nil {
  128. return nil
  129. }
  130. b.vr.req.Resource = ":" + strings.Join(b.vr.resourcesParts, ":") + ":"
  131. if len(b.vr.subjects) == 0 && b.builder.subjectsFunc != nil {
  132. b.vr.subjects = b.builder.subjectsFunc(b.vr.r)
  133. }
  134. if len(b.vr.subjects) == 0 {
  135. b.vr.subjects = []string{Anonymous}
  136. }
  137. if b.builder.contextFunc != nil {
  138. newContext := b.builder.contextFunc(b.vr.r, b.vr.objs)
  139. if newContext != nil {
  140. for k, v := range b.vr.req.Context {
  141. newContext[k] = v
  142. }
  143. b.vr.req.Context = newContext
  144. }
  145. }
  146. var err error
  147. // any of the subjects have permission, then have permission
  148. for _, sub := range b.vr.subjects {
  149. b.vr.req.Subject = sub
  150. err = b.builder.ladon.IsAllowed(b.vr.req)
  151. if Verbose {
  152. fmt.Printf("have permission: %+v, req: %#+v\n", err == nil, b.vr.req)
  153. }
  154. if err == nil {
  155. return nil
  156. }
  157. }
  158. return err
  159. }