github.com/pavlo67/common@v0.5.3/common/auth/helpers.go (about)

     1  package auth
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pavlo67/common/common"
     7  	"github.com/pavlo67/common/common/config"
     8  	"github.com/pavlo67/common/common/errors"
     9  	"github.com/pavlo67/common/common/rbac"
    10  )
    11  
    12  func Auth(env config.Envs, authOp Operator, role rbac.Role) (actor *Actor, err error) {
    13  
    14  	var actors []Actor
    15  
    16  	if err := env.Value("actors", &actors); err != nil {
    17  		return nil, err
    18  	}
    19  
    20  	for _, actor := range actors {
    21  		if actor.Identity != nil && actor.Identity.Roles.Has(role) {
    22  			actorAuthenticated, err := authOp.Authenticate(actor.Creds)
    23  
    24  			if err != nil {
    25  				return nil, err
    26  			}
    27  			return actorAuthenticated, nil
    28  		}
    29  	}
    30  
    31  	return nil, errors.Wrapf(common.ErrNotFound, fmt.Sprintf("actor with role %s isn't found", role))
    32  }