Skip to content

Commit

Permalink
Merge branch 'main' into feat/storybook-styled-themeing
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite committed Jun 8, 2023
2 parents 458880e + 90c38eb commit cb2b885
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 171 deletions.
13 changes: 5 additions & 8 deletions server/internal/adapter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/reearth/reearth/server/internal/usecase"
"github.com/reearth/reearth/server/internal/usecase/interfaces"
"github.com/reearth/reearth/server/pkg/user"
"github.com/reearth/reearthx/appx"
"golang.org/x/text/language"
)

Expand All @@ -14,7 +15,7 @@ type ContextKey string
const (
contextUser ContextKey = "user"
contextOperator ContextKey = "operator"
contextAuthInfo ContextKey = "authinfo"
ContextAuthInfo ContextKey = "authinfo"
contextUsecases ContextKey = "usecases"
)

Expand All @@ -37,10 +38,6 @@ func AttachOperator(ctx context.Context, o *usecase.Operator) context.Context {
return context.WithValue(ctx, contextOperator, o)
}

func AttachAuthInfo(ctx context.Context, a AuthInfo) context.Context {
return context.WithValue(ctx, contextAuthInfo, a)
}

func AttachUsecases(ctx context.Context, u *interfaces.Container) context.Context {
ctx = context.WithValue(ctx, contextUsecases, u)
return ctx
Expand Down Expand Up @@ -82,9 +79,9 @@ func Operator(ctx context.Context) *usecase.Operator {
return nil
}

func GetAuthInfo(ctx context.Context) *AuthInfo {
if v := ctx.Value(contextAuthInfo); v != nil {
if v2, ok := v.(AuthInfo); ok {
func GetAuthInfo(ctx context.Context) *appx.AuthInfo {
if v := ctx.Value(ContextAuthInfo); v != nil {
if v2, ok := v.(appx.AuthInfo); ok {
return &v2
}
}
Expand Down
6 changes: 4 additions & 2 deletions server/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (
"github.com/99designs/gqlgen/graphql/playground"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/reearth/reearth/server/internal/adapter"
http2 "github.com/reearth/reearth/server/internal/adapter/http"
"github.com/reearth/reearth/server/internal/usecase/interactor"
"github.com/reearth/reearthx/appx"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/rerror"
"github.com/samber/lo"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
)

Expand Down Expand Up @@ -52,8 +55,7 @@ func initEcho(ctx context.Context, cfg *ServerConfig) *echo.Echo {
}

e.Use(
jwtEchoMiddleware(cfg),
loadAuthInfoMiddleware(),
echo.WrapMiddleware(lo.Must(appx.AuthMiddleware(cfg.Config.JWTProviders(), adapter.ContextAuthInfo, true))),
attachOpMiddleware(cfg),
)

Expand Down
7 changes: 7 additions & 0 deletions server/internal/app/auth_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import (
"github.com/reearth/reearthx/util"
)

type contextKey string

const (
debugUserHeader = "X-Reearth-Debug-User"
contextUser contextKey = "reearth_user"
)

// load user from db and attach it to context along with operator
// user id can be from debug header or jwt token
// if its new user, create new user and attach it to context
Expand Down
20 changes: 19 additions & 1 deletion server/internal/app/config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/reearth/reearthx/appx"
"github.com/samber/lo"
)

Expand All @@ -17,6 +18,16 @@ type AuthConfig struct {
JWKSURI *string
}

func (a AuthConfig) JWTProvider() appx.JWTProvider {
return appx.JWTProvider{
ISS: a.ISS,
AUD: a.AUD,
ALG: a.ALG,
TTL: a.TTL,
JWKSURI: a.JWKSURI,
}
}

type AuthConfigs []AuthConfig

type AuthProvider interface {
Expand All @@ -40,6 +51,10 @@ func (ipd *AuthConfigs) Decode(value string) error {
return nil
}

func (a AuthConfigs) JWTProviders() []appx.JWTProvider {
return lo.Map(a, func(a AuthConfig, _ int) appx.JWTProvider { return a.JWTProvider() })
}

type Auth0Config struct {
Domain string
Audience string
Expand Down Expand Up @@ -114,6 +129,9 @@ func prepareUrl(url string) string {
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
url = "https://" + url
}
url = strings.TrimSuffix(url, "/")
// url = strings.TrimSuffix(url, "/")
if !strings.HasSuffix(url, "/") {
url = url + "/"
}
return url
}
12 changes: 6 additions & 6 deletions server/internal/app/config/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ import (

func TestAuth0Config_AuthConfigs(t *testing.T) {
assert.Equal(t, AuthConfigs{{
ISS: "https://hoge.auth0.com",
ISS: "https://hoge.auth0.com/",
AUD: []string{"xxx"},
ClientID: lo.ToPtr("yyy"),
}}, Auth0Config{
Domain: "hoge.auth0.com/",
Domain: "hoge.auth0.com",
Audience: "xxx",
ClientID: "yyy",
}.Configs())
assert.Equal(t, AuthConfigs{{
ISS: "https://hoge.auth0.com",
ISS: "https://hoge.auth0.com/",
AUD: []string{"xxx"},
ClientID: lo.ToPtr("zzz"),
}}, Auth0Config{
Domain: "hoge.auth0.com/",
Domain: "hoge.auth0.com",
Audience: "xxx",
WebClientID: "zzz",
}.Configs())
assert.Equal(t, AuthConfigs{{
ISS: "https://hoge.auth0.com",
ISS: "https://hoge.auth0.com/",
AUD: []string{"xxx"},
ClientID: lo.ToPtr("zzz"),
}, {
ISS: "https://hoge.auth0.com",
ISS: "https://hoge.auth0.com/",
AUD: []string{"xxx"},
ClientID: lo.ToPtr("yyy"),
}}, Auth0Config{
Expand Down
5 changes: 5 additions & 0 deletions server/internal/app/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/reearth/reearthx/appx"
"github.com/reearth/reearthx/log"
"github.com/reearth/reearthx/mailer"
"github.com/samber/lo"
Expand Down Expand Up @@ -156,6 +157,10 @@ func (c Config) Auths() (res AuthConfigs) {
return append(res, c.Auth...)
}

func (c Config) JWTProviders() (res []appx.JWTProvider) {
return c.Auths().JWTProviders()
}

func (c Config) AuthForWeb() *AuthConfig {
if ac := c.Auth0.AuthConfigForWeb(); ac != nil {
return ac
Expand Down
8 changes: 4 additions & 4 deletions server/internal/app/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestReadConfig(t *testing.T) {
cfg, err = ReadConfig(false)
assert.NoError(t, err)
assert.Equal(t, AuthConfigs{
{ISS: "https://foo", ClientID: &clientID}, // Auth0
{ISS: "hoge", AUD: []string{"foo"}}, // REEARTH_AUTH_*
localAuth, // local auth srv
{ISS: "bar"}, // REEARTH_AUTH
{ISS: "https://foo/", ClientID: &clientID}, // Auth0
{ISS: "hoge", AUD: []string{"foo"}}, // REEARTH_AUTH_*
localAuth, // local auth srv
{ISS: "bar"}, // REEARTH_AUTH
}, cfg.Auths())
assert.Equal(t, "foo", cfg.Auth_AUD)
assert.Equal(t, map[string]string{}, cfg.Web)
Expand Down
146 changes: 0 additions & 146 deletions server/internal/app/jwt.go

This file was deleted.

4 changes: 2 additions & 2 deletions web/src/beta/lib/core/mantle/evaluator/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from "lodash-es";
import { cloneDeep, pick } from "lodash-es";

import type { EvalContext, EvalResult } from "..";
import {
Expand Down Expand Up @@ -132,7 +132,7 @@ function evalExpression(
try {
if (hasExpression(expressionContainer)) {
const styleExpression = expressionContainer.expression;
const parsedFeature = recursiveJSONParse(feature);
const parsedFeature = recursiveJSONParse(cloneDeep(feature));
if (typeof styleExpression === "undefined") {
return undefined;
} else if (typeof styleExpression === "object" && styleExpression.conditions) {
Expand Down
4 changes: 2 additions & 2 deletions web/src/classic/core/mantle/evaluator/simple/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick } from "lodash-es";
import { cloneDeep, pick } from "lodash-es";

import type { EvalContext, EvalResult } from "..";
import {
Expand Down Expand Up @@ -127,7 +127,7 @@ function evalExpression(
try {
if (hasExpression(expressionContainer)) {
const styleExpression = expressionContainer.expression;
const parsedFeature = recursiveJSONParse(feature);
const parsedFeature = recursiveJSONParse(cloneDeep(feature));
if (typeof styleExpression === "undefined") {
return undefined;
} else if (typeof styleExpression === "object" && styleExpression.conditions) {
Expand Down

0 comments on commit cb2b885

Please sign in to comment.