Use email claim for account GetRequest

This commit is contained in:
Benedikt Kulmann
2020-05-11 13:59:18 +02:00
parent a8c01a4da8
commit c47dc845b1
3 changed files with 11 additions and 60 deletions

View File

@@ -247,7 +247,6 @@ func loadMiddlewares(cfg *config.Config, l log.Logger) alice.Chain {
oidc.Logger(l),
)
// configuredMiddlewares = append(configuredMiddlewares, oidcMW, middleware.AccountUUID)
return alice.New(middleware.RedirectToHTTPS, oidcMW, middleware.AccountUUID)
}

View File

@@ -22,8 +22,7 @@ func AccountUUID(next http.Handler) http.Handler {
if err != nil {
c := acc.NewAccountsService("com.owncloud.accounts", mclient.DefaultClient) // TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
resp, err := c.Get(context.Background(), &acc.GetRequest{
Uuid: "200~a54bf154-e6a5-4e96-851b-a56c9f6c1fce",
// Email: claims.Email // depends on https://github.com/owncloud/ocis-accounts/pull/28
Email: claims.(ocisoidc.StandardClaims).Email,
})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@@ -38,18 +37,18 @@ func AccountUUID(next http.Handler) http.Handler {
// TODO: build JWT and set it, instead of the uuid on that header.
w.Header().Set("x-ocis-accounts-uuid", resp.Payload.Account.Uuid)
}
} else {
uuid, ok := entry.V.(string)
if !ok {
// placeholder. Add more meaningful response
w.WriteHeader(http.StatusInternalServerError)
return
}
uuid, ok := entry.V.(string)
if !ok {
// placeholder. Add more meaningful response
w.WriteHeader(http.StatusInternalServerError)
return
// TODO: build JWT and set it, instead of the uuid on that header.
w.Header().Set("x-ocis-accounts-uuid", uuid)
}
// TODO: build JWT and set it, instead of the uuid on that header.
w.Header().Set("x-ocis-accounts-uuid", uuid)
next.ServeHTTP(w, r)
})
}

View File

@@ -4,14 +4,11 @@ import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"strings"
"time"
oidc "github.com/coreos/go-oidc"
mclient "github.com/micro/go-micro/v2/client"
acc "github.com/owncloud/ocis-accounts/pkg/proto/v0"
"github.com/coreos/go-oidc"
ocisoidc "github.com/owncloud/ocis-pkg/v2/oidc"
"github.com/owncloud/ocis-proxy/pkg/cache"
"golang.org/x/oauth2"
@@ -24,11 +21,6 @@ var (
// svcCache caches requests for given services to prevent round trips to the service
svcCache = cache.NewCache()
accountSvc = "com.owncloud.accounts"
// UUIDKey works as a context key
UUIDKey interface{} = "uuid"
// ClaimsKey works as a context key for user claims
ClaimsKey interface{} = "claims"
)
@@ -121,14 +113,6 @@ func OpenIDConnect(opts ...ocisoidc.Option) func(next http.Handler) http.Handler
ctxWithClaims := context.WithValue(r.Context(), ClaimsKey, claims)
r = r.WithContext(ctxWithClaims)
// add UUID to the request context for the handler to deal with
_, err = uuidFromClaims(claims)
if err != nil {
opt.Logger.Error().Err(err).Interface("account uuid", userInfo).Msg("failed to unmarshal userinfo claims")
w.WriteHeader(http.StatusInternalServerError)
return
}
opt.Logger.Debug().Interface("claims", claims).Interface("userInfo", userInfo).Msg("unmarshalled userinfo")
// store claims in context
// uses the original context, not the one with probably reduced security
@@ -154,34 +138,3 @@ const (
// It is shared between services.
NodeKey = "node"
)
// from the user claims we need to get the uuid from the accounts service
func uuidFromClaims(claims ocisoidc.StandardClaims) (string, error) {
entry, err := svcCache.Get(AccountsKey, claims.Email)
if err != nil {
c := acc.NewAccountsService("com.owncloud.accounts", mclient.DefaultClient) // TODO this won't work with a registry other than mdns. Look into Micro's client initialization.
resp, err := c.Get(context.Background(), &acc.GetRequest{
Uuid: "200~a54bf154-e6a5-4e96-851b-a56c9f6c1fce",
// Email: claims.Email // depends on https://github.com/owncloud/ocis-accounts/pull/28
})
if err != nil {
return "", err
}
// TODO add logging info. Round trip has been made to the accounts service.
err = svcCache.Set(AccountsKey, claims.Email, resp.Payload.Account.Uuid)
if err != nil {
return "", err
}
return resp.Key, nil
}
uuid, ok := entry.V.(string)
if !ok {
return "", fmt.Errorf("unexpected type on cache entry value. Expected string type")
}
// TODO add logging info. Read from cache.
return uuid, nil
}