Last active
July 8, 2022 16:42
-
-
Save jon-whit/bd6fd17de6cb2cd6e2fc21d2b342aa3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lookup | |
import ( | |
"fmt" | |
openfgapb "go.buf.build/openfga/go/openfga/api/openfga/v1" | |
"golang.org/x/sync/errgroup" | |
) | |
func Lookup(req *openfgapb.LookupRequest, srv openfgapb.OpenFGAService_LookupServer) error { | |
storeID := req.GetStoreId() | |
modelID := req.GetAuthorizationModelId() | |
targetObjectType := req.GetObjectType() | |
targetRelation := req.GetRelation() | |
targetUser := req.GetUser() | |
// query all the relationship tuples for the given storeID and targetObjectType | |
iter, err := database.QueryRelationships(storeID, targetObjectType) | |
if err != nil { | |
// handle error | |
} | |
g, ctx := errgroup.WithContext(srv.Context()) | |
g.SetLimit(100) // limit to 100 concurrent checks, for example | |
for iter.Next() { | |
var objectID string | |
err := iter.Scan(&objectID) | |
if err != nil { | |
return err | |
} | |
fn := func() error { | |
object := fmt.Sprintf("%s:%s", targetObjectType, objectID) | |
allowed, err := Check(ctx, storeID, modelID, object, targetRelation, targetUser) | |
if err != nil { | |
return err | |
} | |
if allowed { | |
err := srv.Send(&openfgapb.LookupResponse{ | |
ObjectId: objectID, | |
}) | |
if err != nil { | |
return err | |
} | |
} | |
return nil | |
} | |
g.Go(fn) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment