-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
IAM 655 - roles API
- Loading branch information
Showing
13 changed files
with
3,207 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// SPDX-License-Identifier: AGPL | ||
|
||
package authorization | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
PERMISSION_SEPARATOR = "::" | ||
) | ||
|
||
type Urn struct { | ||
relation string | ||
object string | ||
} | ||
|
||
func (a *Urn) ID() string { | ||
return fmt.Sprintf("%s%s%s", a.relation, PERMISSION_SEPARATOR, a.object) | ||
} | ||
|
||
func (a *Urn) Relation() string { | ||
return a.relation | ||
} | ||
|
||
func (a *Urn) Object() string { | ||
return a.object | ||
} | ||
|
||
func NewUrn(relation, object string) *Urn { | ||
u := new(Urn) | ||
|
||
u.relation = relation | ||
u.object = object | ||
|
||
return u | ||
} | ||
|
||
func NewUrnFromURLParam(ID string) *Urn { | ||
values := strings.Split(ID, PERMISSION_SEPARATOR) | ||
|
||
if len(values) < 2 { | ||
// not a valid Urn | ||
return nil | ||
} | ||
|
||
// use only first two elements | ||
return NewUrn(values[0], values[1]) | ||
} |
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,102 @@ | ||
// Copyright 2024 Canonical Ltd. | ||
// SPDX-License-Identifier: AGPL | ||
|
||
package types | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/canonical/identity-platform-admin-ui/internal/logging" | ||
"github.com/canonical/identity-platform-admin-ui/internal/tracing" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const ( | ||
PAGINATION_HEADER = "X-Token-Pagination" | ||
) | ||
|
||
type TokenPaginator struct { | ||
tokens map[string]string | ||
|
||
tracer tracing.TracingInterface | ||
logger logging.LoggerInterface | ||
} | ||
|
||
func (p *TokenPaginator) LoadFromRequest(ctx context.Context, r *http.Request) error { | ||
_, span := p.tracer.Start(ctx, "types.TokenPaginator.LoadFromRequest") | ||
defer span.End() | ||
|
||
header := r.Header.Get(PAGINATION_HEADER) | ||
|
||
if header == "" { | ||
return nil | ||
} | ||
|
||
tokenMap, err := base64.StdEncoding.DecodeString(header) | ||
|
||
if err != nil { | ||
p.logger.Errorf("issues decoding header: %s", err) | ||
return err | ||
} | ||
|
||
tokens := map[string]string{} | ||
|
||
err = json.Unmarshal(tokenMap, &tokens) | ||
|
||
if err != nil { | ||
p.logger.Errorf("issues parsing header: %s", err) | ||
return err | ||
} | ||
|
||
p.tokens = tokens | ||
|
||
return nil | ||
} | ||
|
||
func (p *TokenPaginator) SetToken(ctx context.Context, key, value string) { | ||
p.tokens[key] = value | ||
} | ||
|
||
func (p *TokenPaginator) GetToken(ctx context.Context, key string) string { | ||
if token, ok := p.tokens[key]; ok { | ||
return token | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func (p *TokenPaginator) GetAllTokens(ctx context.Context) map[string]string { | ||
return p.tokens | ||
} | ||
|
||
func (p *TokenPaginator) PaginationHeader(ctx context.Context) (string, error) { | ||
_, span := p.tracer.Start(ctx, "types.TokenPaginator.PaginationHeader") | ||
defer span.End() | ||
|
||
if len(p.tokens) == 0 { | ||
return "", nil | ||
} | ||
|
||
tokenMap, err := json.Marshal(p.tokens) | ||
|
||
if err != nil { | ||
p.logger.Errorf("issues parsing tokens: %s", err) | ||
return "", err | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(tokenMap), nil | ||
} | ||
|
||
func NewTokenPaginator(tracer trace.Tracer, logger logging.LoggerInterface) *TokenPaginator { | ||
p := new(TokenPaginator) | ||
|
||
p.logger = logger | ||
p.tracer = tracer | ||
p.tokens = make(map[string]string) | ||
|
||
return p | ||
|
||
} |
Oops, something went wrong.