-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Local Authorization Check for RBAC (#157)
Co-authored-by: Logan Gore <lgore@stytch.com>
- Loading branch information
1 parent
02f3a41
commit e07cfc3
Showing
26 changed files
with
413 additions
and
56 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package b2b | ||
|
||
// !!! | ||
// WARNING: This file is autogenerated | ||
// Only modify code within MANUAL() sections | ||
// or your changes may be overwritten later! | ||
// !!! | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/stytchauth/stytch-go/v11/stytch" | ||
"github.com/stytchauth/stytch-go/v11/stytch/b2b/rbac" | ||
) | ||
|
||
type RBACClient struct { | ||
C stytch.Client | ||
} | ||
|
||
func NewRBACClient(c stytch.Client) *RBACClient { | ||
return &RBACClient{ | ||
C: c, | ||
} | ||
} | ||
|
||
func (c *RBACClient) Policy( | ||
ctx context.Context, | ||
body *rbac.PolicyParams, | ||
) (*rbac.PolicyResponse, error) { | ||
var retVal rbac.PolicyResponse | ||
err := c.C.NewRequest( | ||
ctx, | ||
"GET", | ||
"/v1/b2b/rbac/policy", | ||
nil, | ||
nil, | ||
&retVal, | ||
Check failure on line 38 in stytch/b2b/rbac.go GitHub Actions / test (1.18)
|
||
) | ||
return &retVal, err | ||
} | ||
|
||
// MANUAL(PolicyCache)(TYPES) | ||
|
||
type PolicyCache struct { | ||
rbacClient *RBACClient | ||
policy *rbac.Policy | ||
lastUpdatedAt time.Time | ||
} | ||
|
||
const refreshCadence = 5 * time.Minute | ||
|
||
func NewPolicyCache(rbacClient *RBACClient) *PolicyCache { | ||
return &PolicyCache{rbacClient: rbacClient} | ||
} | ||
|
||
func (pc *PolicyCache) Get(ctx context.Context) (*rbac.Policy, error) { | ||
if pc.policy == nil || time.Since(pc.lastUpdatedAt) > refreshCadence { | ||
policyResp, err := pc.rbacClient.Policy(ctx, &rbac.PolicyParams{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pc.policy = policyResp.Policy | ||
pc.lastUpdatedAt = time.Now() | ||
} | ||
return pc.policy, nil | ||
} | ||
|
||
// ENDMANUAL(PolicyCache) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package rbac | ||
|
||
// !!! | ||
// WARNING: This file is autogenerated | ||
// Only modify code within MANUAL() sections | ||
// or your changes may be overwritten later! | ||
// !!! | ||
|
||
type Policy struct { | ||
Roles []PolicyRole `json:"roles,omitempty"` | ||
Resources []PolicyResource `json:"resources,omitempty"` | ||
} | ||
type ( | ||
PolicyParams struct{} | ||
PolicyResource struct { | ||
ResourceID string `json:"resource_id,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Actions []string `json:"actions,omitempty"` | ||
} | ||
) | ||
|
||
type PolicyRole struct { | ||
RoleID string `json:"role_id,omitempty"` | ||
Description string `json:"description,omitempty"` | ||
Permissions []PolicyRolePermission `json:"permissions,omitempty"` | ||
} | ||
type PolicyRolePermission struct { | ||
ResourceID string `json:"resource_id,omitempty"` | ||
Actions []string `json:"actions,omitempty"` | ||
} | ||
type PolicyResponse struct { | ||
RequestID string `json:"request_id,omitempty"` | ||
StatusCode int32 `json:"status_code,omitempty"` | ||
Policy *Policy `json:"policy,omitempty"` | ||
} |
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
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
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
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
Oops, something went wrong.