Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(auth): add authentication endpoints #6265

Merged
merged 83 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
dacdb27
initial implementation
pKorsholm Jan 26, 2024
cd38e4c
add test for invalid scope
pKorsholm Jan 26, 2024
d3fd771
get config from scope not db
pKorsholm Jan 26, 2024
512f5df
assign config from scope
pKorsholm Jan 26, 2024
908e5e6
fix package.json
pKorsholm Jan 26, 2024
4bd6491
optional providers
pKorsholm Jan 26, 2024
9869c71
make providers options
pKorsholm Jan 26, 2024
36d51ef
rename auth to authentication
pKorsholm Jan 26, 2024
7553ea0
more renaming
pKorsholm Jan 26, 2024
ecd2250
update package name
pKorsholm Jan 26, 2024
333a268
add create-customer and customers/me routes
pKorsholm Jan 26, 2024
036e577
add changeset
pKorsholm Jan 26, 2024
9d49a5d
rm auth endpoints from branch
pKorsholm Jan 26, 2024
99c8e2d
cleanup for pr
pKorsholm Jan 26, 2024
94cf842
add fields to customer creation endpoint
pKorsholm Jan 26, 2024
e6baee6
add fields to customer creation endpoint
pKorsholm Jan 26, 2024
76ca08e
delete file
pKorsholm Jan 26, 2024
0418435
update query-config
pKorsholm Jan 26, 2024
fe2c5c3
rename steps
pKorsholm Jan 29, 2024
d8e5415
add create-customer and customers/me routes
pKorsholm Jan 26, 2024
9c6a487
add changeset
pKorsholm Jan 26, 2024
5cc7742
rm auth endpoints from branch
pKorsholm Jan 26, 2024
44dbffd
cleanup for pr
pKorsholm Jan 26, 2024
d33f05d
add fields to customer creation endpoint
pKorsholm Jan 26, 2024
e4b0808
add fields to customer creation endpoint
pKorsholm Jan 26, 2024
560162a
delete file
pKorsholm Jan 26, 2024
2742d5d
update query-config
pKorsholm Jan 26, 2024
b92d8a0
rename steps
pKorsholm Jan 29, 2024
e6a4d59
add auth folders
pKorsholm Jan 26, 2024
58144bb
add middlewares
pKorsholm Jan 29, 2024
5e04717
case as passport
pKorsholm Jan 29, 2024
21d165d
make callback functions arrow-functions and cast "this"
pKorsholm Jan 29, 2024
a51c569
Merge branch 'develop' into feat/add-create-customer-route-to-v2
pKorsholm Jan 29, 2024
6b9c832
rename scope
pKorsholm Jan 29, 2024
96e12e6
update providers for core integration
pKorsholm Jan 29, 2024
6856205
add auth handlers
pKorsholm Jan 29, 2024
9b05eda
add initial middlewares for authentication
pKorsholm Jan 29, 2024
f4b02a0
add scope and medusa_id types to session type
pKorsholm Jan 29, 2024
b58d304
update yarn.lock
pKorsholm Jan 29, 2024
48f41e2
feat: create/update/delete customers
srindom Jan 29, 2024
099f1e0
fix: cleanup directories
srindom Jan 29, 2024
3e0e16d
fix: typo
srindom Jan 29, 2024
6b91e87
fix: faulty export
srindom Jan 29, 2024
201a690
fix: missing export
srindom Jan 29, 2024
6b494a6
Merge remote-tracking branch 'origin/feat/add-create-customer-route-t…
srindom Jan 29, 2024
a6c69e2
create user if no user exists
pKorsholm Jan 30, 2024
1e29439
update creation type for auth-users
pKorsholm Jan 30, 2024
e7e5fde
create-customers route
pKorsholm Jan 30, 2024
7d90532
update provider types
pKorsholm Jan 30, 2024
753e59c
create initial middlewares
pKorsholm Jan 30, 2024
671404f
touch up
pKorsholm Jan 30, 2024
94cc17f
Merge branch 'develop' into feat/add-authentication-endpoints-for-v2
pKorsholm Jan 30, 2024
a8132f4
rm legacy provider
pKorsholm Jan 30, 2024
4f5753d
update middlewares
pKorsholm Jan 30, 2024
dc6e669
feat: poc middleware
srindom Jan 30, 2024
68cd24c
Merge branch 'feat/customer-store' of github.com:medusajs/medusa into…
srindom Jan 30, 2024
decb5db
fix: cleanup
srindom Jan 30, 2024
2afcae8
fix: cleanup
srindom Jan 30, 2024
1fda3f2
feat: /store/customers endpoints
srindom Jan 30, 2024
3e87fea
Merge remote-tracking branch 'origin/develop' into feat/customer-store
srindom Jan 30, 2024
4d61cdd
Merge branch 'develop' into feat/add-authentication-endpoints-for-v2
pKorsholm Jan 31, 2024
06f2adc
Merge branch 'feat/customer-store' into feat/add-authentication-endpo…
pKorsholm Jan 31, 2024
91d46ce
new middleware changes
pKorsholm Jan 31, 2024
42b3a6d
update auth middleware
pKorsholm Jan 31, 2024
ee398a6
Merge branch 'feat/customer-store' into feat/add-authentication-endpo…
pKorsholm Jan 31, 2024
d43cdfb
rm unused files
pKorsholm Jan 31, 2024
eeffa8e
revert changes
pKorsholm Jan 31, 2024
e5715b1
revert changes
pKorsholm Jan 31, 2024
36a6d83
import authentication middleware correctly
pKorsholm Jan 31, 2024
fe6bb3d
rm route
pKorsholm Jan 31, 2024
d0d64ea
cleanup for pr
pKorsholm Jan 31, 2024
bf2ba61
Merge branch 'develop' into feat/add-authentication-endpoints-for-v2
pKorsholm Feb 1, 2024
bab06a2
revert changes
pKorsholm Feb 1, 2024
c837d7a
update request type
pKorsholm Feb 1, 2024
1ecebf4
fix auth integration tests
pKorsholm Feb 1, 2024
be95dce
fix integration tests
pKorsholm Feb 1, 2024
ec43f6f
Feat(auth): Add user scope field (#6288)
pKorsholm Feb 1, 2024
5acba3b
pr feedback
pKorsholm Feb 1, 2024
0937f31
Update packages/medusa/src/api-v2/store/customers/route.ts
pKorsholm Feb 1, 2024
237a802
Update packages/auth/src/models/auth-user.ts
pKorsholm Feb 1, 2024
2f9c25b
throw if unauthorized
pKorsholm Feb 1, 2024
8c41abb
pr feedback
pKorsholm Feb 1, 2024
63adf0b
Merge branch 'develop' into feat/add-authentication-endpoints-for-v2
pKorsholm Feb 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { IAuthModuleService, ICustomerModuleService } from "@medusajs/types"
import { initDb, useDb } from "../../../../environment-helpers/use-db"

import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { ICustomerModuleService, IAuthModuleService } from "@medusajs/types"
import adminSeeder from "../../../../helpers/admin-seeder"
import { getContainer } from "../../../../environment-helpers/use-container"
import path from "path"
import { startBootstrapApp } from "../../../../environment-helpers/bootstrap-app"
import { useApi } from "../../../../environment-helpers/use-api"
import { getContainer } from "../../../../environment-helpers/use-container"
import { initDb, useDb } from "../../../../environment-helpers/use-db"
import adminSeeder from "../../../../helpers/admin-seeder"

jest.setTimeout(50000)

Expand Down Expand Up @@ -49,6 +50,7 @@ describe("POST /store/customers", () => {
const authUser = await authService.createAuthUser({
entity_id: "store_user",
provider_id: "test",
scope: "store",
})
const jwt = await authService.generateJwtToken(authUser.id, "store")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createAuthenticatedCustomer = async (
const authUser = await authService.createAuthUser({
entity_id: "store_user",
provider_id: "test",
scope: "store",
app_metadata: { customer_id: customer.id },
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AuthUser } from "@models"
import { SqlEntityManager } from "@mikro-orm/postgresql"

export async function createAuthUsers(
manager: SqlEntityManager,
Expand All @@ -8,15 +8,18 @@ export async function createAuthUsers(
id: "test-id",
entity_id: "test-id",
provider: "manual",
scope: "store",
},
{
id: "test-id-1",
entity_id: "test-id-1",
provider: "manual",
scope: "store",
},
{
entity_id: "test-id-2",
provider: "store",
scope: "store",
},
]
): Promise<AuthUser[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { AuthUserService } from "@services"

import ContainerLoader from "../../../../src/loaders/container"
import { MikroOrmWrapper } from "../../../utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { asValue } from "awilix"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { createMedusaContainer } from "@medusajs/utils"
import { asValue } from "awilix"
import ContainerLoader from "../../../../src/loaders/container"

jest.setTimeout(30000)

Expand Down Expand Up @@ -229,7 +228,8 @@ describe("AuthUser Service", () => {
{
id: "test",
provider_id: "manual",
entity_id: "test"
entity_id: "test",
scope: "store"
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ describe("AuthModuleService - AuthUser", () => {
id: "test",
provider_id: "manual",
entity_id: "test",
scope: "store",
},
])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe("AuthModuleService - AuthProvider", () => {
const { success, error } = await service.authenticate(
"emailpass",
{
scope: "non-existing",
authScope: "non-existing",
} as any
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
import { MedusaModule, Modules } from "@medusajs/modules-sdk"

import { IAuthModuleService } from "@medusajs/types"
import { MikroOrmWrapper } from "../../../utils"
import Scrypt from "scrypt-kdf"
import { SqlEntityManager } from "@mikro-orm/postgresql"
Expand Down Expand Up @@ -62,6 +62,7 @@ describe("AuthModuleService - AuthProvider", () => {
{
provider: "emailpass",
entity_id: email,
scope: "store",
provider_metadata: {
password: passwordHash,
},
Expand All @@ -73,8 +74,8 @@ describe("AuthModuleService - AuthProvider", () => {
email: "test@test.com",
password: password,
},
scope: "store",
})
authScope: "store",
} as any)

expect(res).toEqual({
success: true,
Expand All @@ -92,8 +93,8 @@ describe("AuthModuleService - AuthProvider", () => {

const res = await service.authenticate("emailpass", {
body: { email: "test@test.com" },
scope: "store",
})
authScope: "store",
} as any)

expect(res).toEqual({
success: false,
Expand All @@ -106,8 +107,8 @@ describe("AuthModuleService - AuthProvider", () => {

const res = await service.authenticate("emailpass", {
body: { password: "supersecret" },
scope: "store",
})
authScope: "store",
} as any)

expect(res).toEqual({
success: false,
Expand All @@ -127,6 +128,7 @@ describe("AuthModuleService - AuthProvider", () => {
// Add authenticated user
{
provider: "emailpass",
scope: "store",
entity_id: email,
provider_metadata: {
password_hash: passwordHash,
Expand All @@ -139,8 +141,8 @@ describe("AuthModuleService - AuthProvider", () => {
email: "test@test.com",
password: "password",
},
scope: "store",
})
authScope: "store",
} as any)

expect(res).toEqual({
success: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@mikro-orm/migrations": "5.9.7",
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"dotenv": "16.3.1",
"jsonwebtoken": "^9.0.2",
"knex": "2.4.2",
"scrypt-kdf": "^2.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,14 @@
"nullable": false,
"mappedType": "text"
},
"domain": {
"name": "domain",
"scope": {
"name": "scope",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"default": "'all'",
"enumItems": [
"all",
"store",
"admin"
],
"mappedType": "enum"
"nullable": true,
"mappedType": "text"
},
"config": {
"name": "config",
Expand Down Expand Up @@ -104,6 +98,15 @@
"nullable": true,
"mappedType": "text"
},
"scope": {
"name": "scope",
"type": "text",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": false,
"mappedType": "text"
},
"user_metadata": {
"name": "user_metadata",
"type": "jsonb",
Expand All @@ -119,7 +122,7 @@
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"nullable": false,
"mappedType": "json"
},
"provider_metadata": {
Expand All @@ -136,9 +139,10 @@
"schema": "public",
"indexes": [
{
"keyName": "IDX_auth_user_provider_entity_id",
"keyName": "IDX_auth_user_provider_scope_entity_id",
"columnNames": [
"provider_id",
"scope",
"entity_id"
],
"composite": true,
Expand Down
30 changes: 0 additions & 30 deletions packages/auth/src/migrations/Migration20240122041959.ts

This file was deleted.

22 changes: 22 additions & 0 deletions packages/auth/src/migrations/Migration20240201100135.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Migration } from '@mikro-orm/migrations';

export class Migration20240201100135 extends Migration {

async up(): Promise<void> {
this.addSql('create table "auth_provider" ("provider" text not null, "name" text not null, "scope" text null, "config" jsonb null, "is_active" boolean not null default false, constraint "auth_provider_pkey" primary key ("provider"));');

this.addSql('create table "auth_user" ("id" text not null, "entity_id" text not null, "provider_id" text null, "scope" text not null, "user_metadata" jsonb null, "app_metadata" jsonb not null, "provider_metadata" jsonb null, constraint "auth_user_pkey" primary key ("id"));');
this.addSql('alter table "auth_user" add constraint "IDX_auth_user_provider_scope_entity_id" unique ("provider_id", "scope", "entity_id");');

this.addSql('alter table "auth_user" add constraint "auth_user_provider_id_foreign" foreign key ("provider_id") references "auth_provider" ("provider") on delete cascade;');
}

async down(): Promise<void> {
this.addSql('alter table "auth_user" drop constraint "auth_user_provider_id_foreign";');

this.addSql('drop table if exists "auth_provider" cascade;');

this.addSql('drop table if exists "auth_user" cascade;');
}

}
4 changes: 2 additions & 2 deletions packages/auth/src/models/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class AuthProvider {
@Property({ columnType: "text" })
name: string

@Enum({ items: () => ProviderDomain, default: ProviderDomain.ALL })
domain: ProviderDomain = ProviderDomain.ALL
@Property({ columnType: "text", nullable: true })
scope: string

@Property({ columnType: "jsonb", nullable: true })
config: Record<string, unknown> | null = null
Expand Down
14 changes: 10 additions & 4 deletions packages/auth/src/models/auth-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import { generateEntityId } from "@medusajs/utils"
type OptionalFields = "provider_metadata" | "app_metadata" | "user_metadata"

@Entity()
@Unique({ properties: ["provider","entity_id" ], name: "IDX_auth_user_provider_entity_id" })
@Unique({
properties: ["provider", "scope", "entity_id"],
name: "IDX_auth_user_provider_scope_entity_id",
})
export default class AuthUser {
[OptionalProps]: OptionalFields

Expand All @@ -34,14 +37,17 @@ export default class AuthUser {
})
provider: AuthProvider

@Property({ columnType: "text" })
scope: string

@Property({ columnType: "jsonb", nullable: true })
user_metadata: Record<string, unknown> | null

@Property({ columnType: "jsonb", nullable: true })
app_metadata: Record<string, unknown> | null
@Property({ columnType: "jsonb" })
app_metadata: Record<string, unknown> = {}

@Property({ columnType: "jsonb", nullable: true })
provider_metadata: Record<string, unknown> | null
provider_metadata: Record<string, unknown> | null = null

@BeforeCreate()
onCreate() {
Expand Down
Loading
Loading