Skip to content

Commit

Permalink
some additional thoughts
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Sep 4, 2020
1 parent 4a62f86 commit fc00e5c
Show file tree
Hide file tree
Showing 12 changed files with 420 additions and 519 deletions.
243 changes: 95 additions & 148 deletions api_client.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions api_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (tp staticTP) TargetRealm() string {
return tp.realm
}

func (tp staticTP) BearerToken() (string, error) {
func (tp staticTP) Current() (string, error) {
return tp.token, nil
}

Expand Down Expand Up @@ -337,7 +337,7 @@ func TestConfidentialClientTokenProvider(t *testing.T) {
}

t.Run("get-token", func(t *testing.T) {
if _, err := tc.TokenProvider().BearerToken(); err != nil {
if _, err := tc.TokenProvider().Current(); err != nil {
t.Logf("Failed to fetch bearer token from provider: %v", err)
t.FailNow()
}
Expand All @@ -346,7 +346,7 @@ func TestConfidentialClientTokenProvider(t *testing.T) {
t.Run("refresh-token", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := tc.TokenProvider().(keycloak.RenewableTokenProvider).Renew(ctx, tc, false); err != nil {
if err := tc.TokenProvider().(keycloak.RenewableBearerTokenProvider).Renew(ctx, tc, false); err != nil {

}
})
Expand Down
64 changes: 28 additions & 36 deletions api_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (
"strconv"
"strings"
"sync/atomic"

"github.com/rs/zerolog"
)

const (
Expand Down Expand Up @@ -50,11 +48,11 @@ type APIRequest struct {
mpw *multipart.Writer
}

func NewAPIRequest(method, uri string) *APIRequest {
func NewAPIRequest(method, requestURL string) *APIRequest {
r := &APIRequest{
id: atomic.AddUint64(&apiRequestID, 1),
method: method,
uri: uri,
uri: requestURL,
queryParameters: make(map[string][]string),
pathParameters: make(map[string]string),
headers: make(url.Values),
Expand Down Expand Up @@ -325,28 +323,21 @@ func (r *APIRequest) CompiledURI() string {
}

// ToHTTP will attempt to construct an executable http.request
func (r *APIRequest) ToHTTP(ctx context.Context, addr string) (*http.Request, error) {
func (r *APIRequest) ToHTTP(ctx context.Context) (*http.Request, error) {
var (
httpRequest *http.Request
err error

compiledURL = r.CompiledURI()
)

// todo: not the biggest fan of this, redesign call structure.
if !strings.HasPrefix(compiledURL, addr) {
compiledURL = fmt.Sprintf(apiRequestURLFormat, addr, compiledURL)
}

if r.mpw != nil {
r.SetHeader(headerKeyContentType, r.mpw.FormDataContentType())
if err = r.mpw.Close(); err != nil {
return nil, fmt.Errorf("error closing multipart writer: %w", err)
}
}

r.Headers().Add("Accept", headerValueApplicationJSON)

if httpRequest, err = http.NewRequestWithContext(ctx, r.method, compiledURL, r.Body()); err != nil {
return nil, err
}
Expand All @@ -360,30 +351,31 @@ func (r *APIRequest) ToHTTP(ctx context.Context, addr string) (*http.Request, er
return httpRequest, nil
}

func (r *APIRequest) MarshalZerologObject(ev *zerolog.Event) {
ev.Uint64("request_id", r.ID())
ev.Str("method", r.Method())
ev.Str("uri", r.URI())
ev.Str("compiled_uri", r.CompiledURI())
ev.Str("body_type", r.BodyType())
tmp := make([]string, 0)
for k := range r.Headers() {
tmp = append(tmp, k)
}
ev.Strs("header_keys", tmp)
tmp = make([]string, 0)
for k := range r.QueryParameters() {
tmp = append(tmp, k)
}
ev.Strs("query_keys", tmp)
tmp = make([]string, 0)
for k := range r.PathParameters() {
tmp = append(tmp, k)
}
ev.Strs("path_keys", tmp)
ev.Int("cookies", len(r.Cookies()))
ev.Bool("is_multipart", r.mpw != nil)
}
//
//func (r *APIRequest) MarshalZerologObject(ev *zerolog.Event) {
// ev.Uint64("request_id", r.ID())
// ev.Str("method", r.Method())
// ev.Str("uri", r.URI())
// ev.Str("compiled_uri", r.CompiledURI())
// ev.Str("body_type", r.BodyType())
// tmp := make([]string, 0)
// for k := range r.Headers() {
// tmp = append(tmp, k)
// }
// ev.Strs("header_keys", tmp)
// tmp = make([]string, 0)
// for k := range r.QueryParameters() {
// tmp = append(tmp, k)
// }
// ev.Strs("query_keys", tmp)
// tmp = make([]string, 0)
// for k := range r.PathParameters() {
// tmp = append(tmp, k)
// }
// ev.Strs("path_keys", tmp)
// ev.Int("cookies", len(r.Cookies()))
// ev.Bool("is_multipart", r.mpw != nil)
//}

func addContentDispositionHeader(req *APIRequest, key, filename string) {
req.AddHeader(
Expand Down
65 changes: 65 additions & 0 deletions cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package keycloak

import (
"fmt"
"strings"
"time"

"github.com/dcarbone/sclg/v3"
"github.com/google/go-cmp/cmp"
)

type CacheBackend interface {
Load(key interface{}) (value interface{}, ok bool)
StoreUntil(key, value interface{}, deadline time.Time)
Delete(key interface{})
Flush() int
}

var (
globalCache *sclg.TimedCache
)

func init() {
conf := new(sclg.TimedCacheConfig)
conf.Comparator = globalCacheEquivalencyTest
globalCache = sclg.NewTimedCache(conf)
}

func globalCacheEquivalencyTest(_, current, new interface{}) bool {
return cmp.Equal(current, new)
}

// buildPKCacheKey creates the public key cache entry keys.
func buildPKCacheKey(authServerURL, realm, keyID string) string {
return fmt.Sprintf(pkKeyFormat, authServerURL, realm, keyID)
}

// parsePKCacheKey splits a cache key into authServerURL : realm : keyID
func parsePKCacheKey(key interface{}) (string, string, string) {
str, ok := key.(string)
if !ok {
return "", "", ""
}
s := strings.SplitN("\n", str, 4)
if len(s) != 4 || s[0] != pkKeyPrefix {
return "", "", ""
}
return s[1], s[2], s[3]
}

func buildRealmEnvCacheKey(authServerURL, realm string) string {
return fmt.Sprintf(reKeyFormat, authServerURL, realm)
}

func parseRealmEnvCacheKey(key interface{}) (string, string) {
str, ok := key.(string)
if !ok {
return "", ""
}
s := strings.SplitN(str, "\n", 3)
if len(s) != 3 || s[0] != reKeyPrefix {
return "", ""
}
return s[1], s[2]
}
46 changes: 0 additions & 46 deletions provider_auth_server_url.go

This file was deleted.

40 changes: 0 additions & 40 deletions provider_realm.go

This file was deleted.

Loading

0 comments on commit fc00e5c

Please sign in to comment.