Skip to content

Commit

Permalink
wip rework
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed Dec 17, 2023
1 parent 79d84df commit f1eec2a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
9 changes: 9 additions & 0 deletions util/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package util

type Set[T comparable] map[T]struct{}

func (s Set[T]) Add(v T) bool {
_, present := s[v]
s[v] = struct{}{}
return !present
}
31 changes: 24 additions & 7 deletions web/dev_application_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/http"
"net/url"
"slices"
"strings"
"time"
)

Expand Down Expand Up @@ -68,7 +69,12 @@ func CreateDevApplicationClientEndpoint() echo.HandlerFunc {
return echo.NewHTTPError(http.StatusBadRequest, errors.New("displayname must be between 1 and 100 characters"))
}

body.RedirectURIs = util.Unique(body.RedirectURIs)
applicationClientId, err := uuid.NewV4()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

body.RedirectURIs = preprocessRedirectURIs(applicationClientId, body.RedirectURIs)
if len(body.RedirectURIs) < 1 || len(body.RedirectURIs) > 50 {
return echo.NewHTTPError(http.StatusBadRequest, errors.New("at least one and at most 50 redirect URIs might be added"))
}
Expand All @@ -78,11 +84,6 @@ func CreateDevApplicationClientEndpoint() echo.HandlerFunc {
}

creationTime := time.Now()
applicationClientId, err := uuid.NewV4()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

clientSecret, err := generateClientSecret()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
Expand All @@ -94,7 +95,6 @@ func CreateDevApplicationClientEndpoint() echo.HandlerFunc {
}

ctx := c.Request().Context()

var created bool
err = rctx.ExecuteTx(ctx, pgx.TxOptions{}, func(tx pgx.Tx) error {
const sql = `
Expand Down Expand Up @@ -435,6 +435,23 @@ AND application_client_accounts.account_id = app_client_account.account_id
})
}

func preprocessRedirectURIs(clientId uuid.UUID, redirectURIs []string) []string {
const placeholder = "$client_id"

clientIdStr := clientId.String()
r := make([]string, 0, len(redirectURIs))
unq := make(util.Set[string])

for _, redirectURI := range redirectURIs {
redirectURI = strings.ReplaceAll(redirectURI, placeholder, clientIdStr)
if unq.Add(redirectURI) {
r = append(r, redirectURI)
}
}

return r
}

func validateRedirectURIs(redirectURIs []string) error {
var err error
for _, redirectURI := range redirectURIs {
Expand Down

0 comments on commit f1eec2a

Please sign in to comment.