Explorar o código

Move collection to a new file.

Mariano Cano %!s(int64=7) %!d(string=hai) anos
pai
achega
dd0376657c
Modificáronse 2 ficheiros con 48 adicións e 35 borrados
  1. 41 0
      authority/provisioner/collection.go
  2. 7 35
      authority/provisioner/provisioner.go

+ 41 - 0
authority/provisioner/collection.go

@@ -0,0 +1,41 @@
+package provisioner
+
+import (
+	"sync"
+
+	"github.com/pkg/errors"
+)
+
+// Collection is a memory map of provisioners.
+type Collection struct {
+	byID *sync.Map
+}
+
+// NewCollection initializes a collection of provisioners.
+func NewCollection() *Collection {
+	return &Collection{
+		byID: new(sync.Map),
+	}
+}
+
+// Load a provisioner by the ID.
+func (c *Collection) Load(id string) (*Provisioner, bool) {
+	i, ok := c.byID.Load(id)
+	if !ok {
+		return nil, false
+	}
+	p, ok := i.(*Provisioner)
+	if !ok {
+		return nil, false
+	}
+	return p, true
+}
+
+// Store adds a provisioner to the collection, it makes sure two provisioner
+// does not have the same ID.
+func (c *Collection) Store(p *Provisioner) error {
+	if _, loaded := c.byID.LoadOrStore(p.ID(), p); loaded == false {
+		return errors.New("cannot add multiple provisioners with the same id")
+	}
+	return nil
+}

+ 7 - 35
authority/provisioner/provisioner.go

@@ -3,7 +3,6 @@ package provisioner
 import (
 import (
 	"encoding/json"
 	"encoding/json"
 	"strings"
 	"strings"
-	"sync"
 
 
 	"github.com/pkg/errors"
 	"github.com/pkg/errors"
 )
 )
@@ -44,6 +43,11 @@ func (p *Provisioner) ID() string {
 	return p.base.ID()
 	return p.base.ID()
 }
 }
 
 
+// Type return the provisioners type.
+func (p *Provisioner) Type() Type {
+	return p.typ
+}
+
 // Init initializes the base provisioner with the given claims.
 // Init initializes the base provisioner with the given claims.
 func (p *Provisioner) Init(claims *Claims) error {
 func (p *Provisioner) Init(claims *Claims) error {
 	return p.base.Init(claims)
 	return p.base.Init(claims)
@@ -70,8 +74,10 @@ func (p *Provisioner) UnmarshalJSON(data []byte) error {
 
 
 	switch strings.ToLower(typ.Type) {
 	switch strings.ToLower(typ.Type) {
 	case "jwt":
 	case "jwt":
+		p.typ = TypeJWK
 		p.base = &JWT{}
 		p.base = &JWT{}
 	case "oidc":
 	case "oidc":
+		p.typ = TypeOIDC
 		p.base = &OIDC{}
 		p.base = &OIDC{}
 	default:
 	default:
 		return errors.New("provisioner type not supported")
 		return errors.New("provisioner type not supported")
@@ -81,37 +87,3 @@ func (p *Provisioner) UnmarshalJSON(data []byte) error {
 	}
 	}
 	return nil
 	return nil
 }
 }
-
-// Collection is a memory map of provisioners.
-type Collection struct {
-	byID *sync.Map
-}
-
-// NewCollection initializes a collection of provisioners.
-func NewCollection() *Collection {
-	return &Collection{
-		byID: new(sync.Map),
-	}
-}
-
-// Load a provisioner by the ID.
-func (c *Collection) Load(id string) (*Provisioner, bool) {
-	i, ok := c.byID.Load(id)
-	if !ok {
-		return nil, false
-	}
-	p, ok := i.(*Provisioner)
-	if !ok {
-		return nil, false
-	}
-	return p, true
-}
-
-// Store adds a provisioner to the collection, it makes sure two provisioner
-// does not have the same ID.
-func (c *Collection) Store(p *Provisioner) error {
-	if _, loaded := c.byID.LoadOrStore(p.ID(), p); loaded == false {
-		return errors.New("cannot add multiple provisioners with the same id")
-	}
-	return nil
-}