Пример #1
0
// NewACLMongoAuthorizer creates a new ACL MongoDB authorizer
func NewACLMongoAuthorizer(c *ACLMongoConfig) (Authorizer, error) {
	// Attempt to create new MongoDB session.
	session, err := mgo_session.New(c.MongoConfig)
	if err != nil {
		return nil, err
	}

	authorizer := &aclMongoAuthorizer{
		config:       c,
		session:      session,
		updateTicker: time.NewTicker(c.CacheTTL),
	}

	// Initially fetch the ACL from MongoDB
	if err := authorizer.updateACLCache(); err != nil {
		return nil, err
	}

	go authorizer.continuouslyUpdateACLCache()

	return authorizer, nil
}
Пример #2
0
func NewMongoAuth(c *MongoAuthConfig) (*MongoAuth, error) {
	// Attempt to create new mongo session.
	session, err := mgo_session.New(c.MongoConfig)
	if err != nil {
		return nil, err
	}

	// Copy our session
	tmp_session := session.Copy()
	// Close up when we are done
	defer tmp_session.Close()

	// determine collection
	collection := tmp_session.DB(c.MongoConfig.DialInfo.Database).C(c.Collection)

	// Create username index obj
	index := mgo.Index{
		Key:      []string{"username"},
		Unique:   true,
		DropDups: false, // Error on duplicate key document instead of drop.
	}

	// Enforce a username index. This is fine to do frequently per the docs:
	// https://godoc.org/gopkg.in/mgo.v2#Collection.EnsureIndex:
	//    Once EnsureIndex returns successfully, following requests for the same index
	//    will not contact the server unless Collection.DropIndex is used to drop the same
	//    index, or Session.ResetIndexCache is called.
	if err := collection.EnsureIndex(index); err != nil {
		return nil, err
	}

	return &MongoAuth{
		config:  c,
		session: session,
	}, nil
}