Skip to content

Commit

Permalink
# This is a combination of 2 commits.
Browse files Browse the repository at this point in the history
# This is the 1st commit message:

implement oidc

Signed-off-by: bakito <[email protected]>

# This is the commit message #2:

return OIDCMapping for teams
  • Loading branch information
bakito committed Dec 4, 2023
1 parent 3f60a93 commit 36062fc
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 4 deletions.
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Client struct {
Finding FindingService
License LicenseService
Metrics MetricsService
OIDC OIDCService
Permission PermissionService
Policy PolicyService
PolicyCondition PolicyConditionService
Expand Down Expand Up @@ -86,6 +87,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
client.Finding = FindingService{client: &client}
client.License = LicenseService{client: &client}
client.Metrics = MetricsService{client: &client}
client.OIDC = OIDCService{client: &client}
client.Permission = PermissionService{client: &client}
client.Policy = PolicyService{client: &client}
client.PolicyCondition = PolicyConditionService{client: &client}
Expand Down Expand Up @@ -251,6 +253,13 @@ func withPageOptions(po PageOptions) requestOption {
}
}

func withAcceptContentType(contentType string) requestOption {
return func(req *http.Request) error {
req.Header.Set("Accept", contentType)
return nil
}
}

func (c Client) doRequest(req *http.Request, v interface{}) (a apiResponse, err error) {
if c.debug {
reqDump, _ := httputil.DumpRequestOut(req, true)
Expand Down
124 changes: 124 additions & 0 deletions oidc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package dtrack

import (
"context"
"fmt"
"net/http"
"strconv"

"github.com/google/uuid"
)

type OIDCService struct {
client *Client
}

type OIDCGroup struct {
Name string `json:"name,omitempty"`
UUID uuid.UUID `json:"uuid,omitempty"`
}

type OIDCMappingRequest struct {
Team string `json:"team"`
Group string `json:"group"`
}

type OIDCMapping struct {
Group OIDCGroup `json:"group"`
UUID uuid.UUID `json:"uuid"`
}

func (s OIDCService) Available(ctx context.Context) (available bool, err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/available", withAcceptContentType("text/plain"))
if err != nil {
return
}

var value string

_, err = s.client.doRequest(req, &value)
if err != nil {
return
}
available, err = strconv.ParseBool(value)
return
}

func (s OIDCService) GetAllGroups(ctx context.Context, po PageOptions) (p Page[OIDCGroup], err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, "/api/v1/oidc/group", withPageOptions(po))
if err != nil {
return
}

res, err := s.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (s OIDCService) CreateGroup(ctx context.Context, name string) (g OIDCGroup, err error) {
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/group", withBody(OIDCGroup{Name: name}))
if err != nil {
return
}

_, err = s.client.doRequest(req, &g)
return
}
func (s OIDCService) UpdateGroup(ctx context.Context, group OIDCGroup) (g OIDCGroup, err error) {
req, err := s.client.newRequest(ctx, http.MethodPost, "/api/v1/oidc/group", withBody(group))
if err != nil {
return
}

_, err = s.client.doRequest(req, &g)
return
}

func (s OIDCService) DeleteGroup(ctx context.Context, id string) (err error) {
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/group/%s", id))
if err != nil {
return
}

_, err = s.client.doRequest(req, nil)
return
}

func (s OIDCService) GetAllTeamsOf(ctx context.Context, group OIDCGroup, po PageOptions) (p Page[Team], err error) {
req, err := s.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/oidc/group/%s/team", group.UUID.String()), withPageOptions(po))
if err != nil {
return
}

res, err := s.client.doRequest(req, &p.Items)
if err != nil {
return
}

p.TotalCount = res.TotalCount
return
}

func (s OIDCService) AddTeamMapping(ctx context.Context, mapping OIDCMappingRequest) (m OIDCMapping, err error) {
req, err := s.client.newRequest(ctx, http.MethodPut, "/api/v1/oidc/mapping", withBody(mapping))
if err != nil {
return
}

_, err = s.client.doRequest(req, &m)
return
}

func (s OIDCService) RemoveTeamMapping(ctx context.Context, mappingID string) (err error) {
req, err := s.client.newRequest(ctx, http.MethodDelete, fmt.Sprintf("/api/v1/oidc/mapping/%s", mappingID))
if err != nil {
return
}

_, err = s.client.doRequest(req, nil)
return
}
9 changes: 5 additions & 4 deletions team.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
)

type Team struct {
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
APIKeys []APIKey `json:"apiKeys,omitempty"`
Permissions []Permission `json:"permissions"`
UUID uuid.UUID `json:"uuid,omitempty"`
Name string `json:"name,omitempty"`
APIKeys []APIKey `json:"apiKeys,omitempty"`
Permissions []Permission `json:"permissions"`
MappedOIDCGroups []OIDCMapping `json:"mappedOidcGroups,omitempty"`
}

type APIKey struct {
Expand Down

0 comments on commit 36062fc

Please sign in to comment.