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(authentication, types): split authenticate method into two #6184

Merged
merged 60 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
72f0e14
init
pKorsholm Jan 9, 2024
f8b7089
fetch providers using updated string
pKorsholm Jan 9, 2024
5adca1c
update loaders
pKorsholm Jan 9, 2024
276f3eb
add more tests
pKorsholm Jan 10, 2024
01578e7
add authenticationresponse type
pKorsholm Jan 11, 2024
757039d
update types for authentication method
pKorsholm Jan 11, 2024
2adb779
add entity_id and update provider
pKorsholm Jan 15, 2024
6db3a46
update pr with return type
pKorsholm Jan 17, 2024
0bf5230
create loaders onApplicationStart
pKorsholm Jan 17, 2024
8b2f0f1
cleanup provider class
pKorsholm Jan 17, 2024
eda5647
run application start hook before each
pKorsholm Jan 17, 2024
3e99dff
fix pr feedback
pKorsholm Jan 22, 2024
5bf05cc
create private onApplicationStart method
pKorsholm Jan 22, 2024
f5c4c1f
Merge branch 'develop' into feat/authentication-provider-username-pas…
pKorsholm Jan 22, 2024
306c972
assign repository
pKorsholm Jan 22, 2024
80e7012
init
pKorsholm Jan 17, 2024
e7bd8cd
add entity_id and update provider
pKorsholm Jan 15, 2024
465cfa7
initial implementation
pKorsholm Jan 17, 2024
4bb7889
update lockfile
pKorsholm Jan 17, 2024
1ffff8a
fix conflicts
pKorsholm Jan 17, 2024
0b1f784
add config variables
pKorsholm Jan 17, 2024
620d93d
update types
pKorsholm Jan 17, 2024
9e916dc
refactor google provider
pKorsholm Jan 17, 2024
b4bf540
re-order methods
pKorsholm Jan 17, 2024
4aa760f
fix pr feedback p. 1
pKorsholm Jan 22, 2024
63a85c9
add initial type and update callback authorization
pKorsholm Jan 22, 2024
c874504
add google provider to integration test
pKorsholm Jan 22, 2024
9a17515
fix feedback
pKorsholm Jan 23, 2024
020b845
initial implementation (#6171)
pKorsholm Jan 23, 2024
dbac0ee
move abstract authentication provider
pKorsholm Jan 23, 2024
fb3fef2
shuffle files around
pKorsholm Jan 23, 2024
08ba487
init
pKorsholm Jan 17, 2024
38ee8e3
add entity_id and update provider
pKorsholm Jan 15, 2024
234302b
initial implementation
pKorsholm Jan 17, 2024
310b0e5
update lockfile
pKorsholm Jan 17, 2024
433642b
fix conflicts
pKorsholm Jan 17, 2024
9010d38
add config variables
pKorsholm Jan 17, 2024
75a80fa
update types
pKorsholm Jan 17, 2024
cc9f58e
refactor google provider
pKorsholm Jan 17, 2024
1d9949c
re-order methods
pKorsholm Jan 17, 2024
c93a54a
fix pr feedback p. 1
pKorsholm Jan 22, 2024
ebe0476
add initial type and update callback authorization
pKorsholm Jan 22, 2024
ca53bc1
add google provider to integration test
pKorsholm Jan 22, 2024
5427e72
fix feedback
pKorsholm Jan 23, 2024
92563a3
initial implementation (#6171)
pKorsholm Jan 23, 2024
6c4d0cf
move abstract authentication provider
pKorsholm Jan 23, 2024
eb7cb1a
shuffle files around
pKorsholm Jan 23, 2024
35f1312
Merge branch 'feat/google-authentication-provider' into feat/split-au…
pKorsholm Jan 23, 2024
758c675
Update packages/authentication/src/migrations/Migration20240122041959.ts
pKorsholm Jan 23, 2024
438d309
Merge branch 'develop' into feat/google-authentication-provider
pKorsholm Jan 23, 2024
0b11ef5
split authentication methods
pKorsholm Jan 23, 2024
742adff
call verify with token
pKorsholm Jan 23, 2024
8bbe3d1
Merge branch 'feat/google-authentication-provider' into feat/split-au…
pKorsholm Jan 23, 2024
072bf9b
update integration tests
pKorsholm Jan 23, 2024
8737689
feedback
pKorsholm Jan 24, 2024
7c73ebc
Merge branch 'develop' into feat/split-authentication-methods
pKorsholm Jan 25, 2024
03475a4
rename split methods
pKorsholm Jan 25, 2024
0c2a711
fix provider integration test
pKorsholm Jan 25, 2024
1af194c
Merge branch 'develop' into feat/split-authentication-methods
pKorsholm Jan 25, 2024
ba264b9
Merge branch 'develop' into feat/split-authentication-methods
pKorsholm Jan 26, 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,10 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"

import { MikroOrmWrapper } from "../../../utils"
import { initialize } from "../../../../src"
import { DB_URL } from "@medusajs/pricing/integration-tests/utils"
import { MedusaModule } from "@medusajs/modules-sdk"
import { IAuthenticationModuleService } from "@medusajs/types"
import { MedusaModule } from "@medusajs/modules-sdk"
import { MikroOrmWrapper } from "../../../utils"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { initialize } from "../../../../src"

jest.setTimeout(30000)

Expand All @@ -24,7 +23,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
},
})

if(service.__hooks?.onApplicationStart) {
if (service.__hooks?.onApplicationStart) {
await service.__hooks.onApplicationStart()
}
})
Expand All @@ -39,12 +38,18 @@ describe("AuthenticationModuleService - AuthProvider", () => {
const authProviders = await service.listAuthProviders()
const serialized = JSON.parse(JSON.stringify(authProviders))

expect(serialized).toEqual([
expect.objectContaining({
provider: "usernamePassword",
name: "Username/Password Authentication",
}),
])
expect(serialized).toEqual(
expect.arrayContaining([
expect.objectContaining({
provider: "usernamePassword",
name: "Username/Password Authentication",
}),
expect.objectContaining({
provider: "google",
name: "Google Authentication",
}),
])
)
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { SqlEntityManager } from "@mikro-orm/postgresql"
import Scrypt from "scrypt-kdf"

import { MikroOrmWrapper } from "../../../utils"
import { initialize } from "../../../../src"
import { DB_URL } from "@medusajs/pricing/integration-tests/utils"
import { MedusaModule } from "@medusajs/modules-sdk"
import { IAuthenticationModuleService } from "@medusajs/types"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { MedusaModule } from "@medusajs/modules-sdk"
import { MikroOrmWrapper } from "../../../utils"
import Scrypt from "scrypt-kdf"
import { SqlEntityManager } from "@mikro-orm/postgresql"
import { createAuthProviders } from "../../../__fixtures__/auth-provider"
import { createAuthUsers } from "../../../__fixtures__/auth-user"
import { initialize } from "../../../../src"

jest.setTimeout(30000)
const seedDefaultData = async (testManager) => {
Expand Down Expand Up @@ -60,7 +59,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
},
])

const res = await service.authenticate("usernamePassword", {
const res = await service.initializeAuthentication("usernamePassword", {
body: {
email: "test@test.com",
password: password,
Expand All @@ -82,7 +81,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {

await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
const res = await service.initializeAuthentication("usernamePassword", {
body: { email: "test@test.com" },
})

Expand All @@ -95,7 +94,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
it("fails when no email is given", async () => {
await seedDefaultData(testManager)

const res = await service.authenticate("usernamePassword", {
const res = await service.initializeAuthentication("usernamePassword", {
body: { password: "supersecret" },
})

Expand Down Expand Up @@ -124,7 +123,7 @@ describe("AuthenticationModuleService - AuthProvider", () => {
},
])

const res = await service.authenticate("usernamePassword", {
const res = await service.initializeAuthentication("usernamePassword", {
body: {
email: "test@test.com",
password: "password",
Expand Down
4 changes: 3 additions & 1 deletion packages/authentication/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
"@mikro-orm/postgresql": "5.9.7",
"awilix": "^8.0.0",
"dotenv": "^16.1.4",
"jsonwebtoken": "^9.0.2",
"knex": "2.4.2",
"scrypt-kdf": "^2.0.1"
"scrypt-kdf": "^2.0.1",
"simple-oauth2": "^5.0.0"
}
}
13 changes: 10 additions & 3 deletions packages/authentication/src/loaders/providers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import * as defaultProviders from "@providers"

import {
AwilixContainer,
ClassOrFunctionReturning,
Constructor,
Resolver,
asClass,
} from "awilix"
import { LoaderOptions, ModulesSdkTypes } from "@medusajs/types"

import { AwilixContainer, ClassOrFunctionReturning, Resolver, asClass, asFunction, asValue } from "awilix"

export default async ({
container,
}: LoaderOptions<
Expand All @@ -18,7 +23,9 @@ export default async ({

for (const provider of providersToLoad) {
container.register({
[`auth_provider_${provider.PROVIDER}`]: asClass(provider).singleton(),
[`auth_provider_${provider.PROVIDER}`]: asClass(
provider as Constructor<any>
).singleton(),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
],
"mappedType": "enum"
},
"config": {
"name": "config",
"type": "jsonb",
"unsigned": false,
"autoincrement": false,
"primary": false,
"nullable": true,
"mappedType": "json"
},
"is_active": {
"name": "is_active",
"type": "boolean",
Expand Down
21 changes: 0 additions & 21 deletions packages/authentication/src/migrations/Migration20240104154451.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/authentication/src/migrations/Migration20240115092929.ts

This file was deleted.

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

export class Migration20240122041959 extends Migration {

async up(): Promise<void> {
this.addSql('create table if not exists "auth_provider" ("provider" text not null, "name" text not null, "domain" text check ("domain" in (\'all\', \'store\', \'admin\')) not null default \'all\', "config" jsonb null, "is_active" boolean not null default false, constraint "auth_provider_pkey" primary key ("provider"));');

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

this.addSql('alter table "auth_user" add constraint if not exists "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 if exists "auth_user_provider_id_foreign";');

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

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

}
6 changes: 5 additions & 1 deletion packages/authentication/src/models/auth-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import {
PrimaryKey,
Property,
} from "@mikro-orm/core"

import { ProviderDomain } from "../types/repositories/auth-provider"

type OptionalFields = "domain" | "is_active"
type OptionalFields = "domain" | "is_active" | "config"

@Entity()
export default class AuthProvider {
Expand All @@ -22,6 +23,9 @@ export default class AuthProvider {
@Enum({ items: () => ProviderDomain, default: ProviderDomain.ALL })
domain: ProviderDomain = ProviderDomain.ALL

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

@Property({ columnType: "boolean", default: false })
is_active = false
}
Loading