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

Chore(mygovid): set fixed oid and sub #73

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Default config
TRUST_PROXY_HEADER=1
DB_URL=postgresql://postgres:p0stgr3s@localhost:5433/logto
ADMIN_PORT=3302
PORT=3301

# OGCIO Config
MOCK_TOKEN_ENDPOINT=http://localhost:4005/logto/mock/token
MOCK_KEYS_ENDPOINT=http://localhost:4005/logto/mock/keys
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ run:
down:
docker-compose -f docker-compose-local.yml down
run-native:
@echo "${GREEN}Copying .env file...${NC}"
cp -- ".env.sample" ".env"
@echo "${GREEN}Copied!${NC}"
@echo "${GREEN}Starting db...${NC}"
docker compose -f docker-compose-local.yml up --detach postgres
@echo "${GREEN}Db started!${NC}"
Expand Down
30 changes: 20 additions & 10 deletions mygovid-mock-service/src/routes/logto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ export default async function login(app: FastifyInstance) {
const { redirect_uri, state } = request.query;

const stream = fs.createReadStream(
path.join(__dirname, "..", "static", "mock-login.html")
path.join(__dirname, "..", "static", "mock-login.html"),
);

const result = (await streamToString(stream))
.replace("%REDIRECT_URL%", redirect_uri)
.replace("%STATE%", state);
return reply.type("text/html").send(result);
}
},
);

app.post<{
Expand All @@ -60,19 +60,29 @@ export default async function login(app: FastifyInstance) {
email: string;
redirect_url: string;
state: string;
sub: string;
oid: string;
};
}>("/login", async (request, reply) => {
const { password, firstName, lastName, email, redirect_url, state } =
request.body;
const {
password,
firstName,
lastName,
email,
redirect_url,
state,
sub,
oid,
} = request.body;

if (password !== "123")
reply.redirect(
`/logto/mock/auth?redirect_uri=${redirect_url}&state=${state}`
`/logto/mock/auth?redirect_uri=${redirect_url}&state=${state}`,
);

const id_token = await createMockSignedJwt(
{ firstName, lastName, email },
request.headers.origin as unknown as string
{ firstName, lastName, email, sub, oid },
request.headers.origin as unknown as string,
);

return reply.redirect(`${redirect_url}?code=${id_token}&state=${state}`);
Expand Down Expand Up @@ -138,7 +148,7 @@ export default async function login(app: FastifyInstance) {
"eyJ2ZXIiOiIxLjAiLCJ0aWQiOiI4OTc5MmE2ZC0xZWE0LTQxMjYtOTRkZi1hNzFkMjkyZGViYzciLCJzdWIiOm51bGwsIm5hbWUiOm51bGwsInByZWZlcnJlZF91c2VybmFtZSI6bnVsbCwiaWRwIjpudWxsfQ",
scope: "openid",
};
}
},
);

app.get<{
Expand All @@ -164,7 +174,7 @@ export default async function login(app: FastifyInstance) {
kty: Type.Optional(Type.String()),
n: Type.Optional(Type.String()),
e: Type.Optional(Type.String()),
})
}),
),
}),
500: HttpError,
Expand All @@ -178,6 +188,6 @@ export default async function login(app: FastifyInstance) {
return {
keys: [{ kid: "signingkey.mygovid.v1", use: "sig", kty, n, e }],
};
}
},
);
}
6 changes: 4 additions & 2 deletions mygovid-mock-service/src/routes/logto/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ export const createMockSignedJwt = async (
firstName: string;
lastName: string;
email: string;
sub: string;
oid: string;
},
origin: string,
) => {
const body = {
ver: "1.0",
sub: getRandomString(),
sub: user.sub,
auth_time: Date.now(),
email: user.email,
oid: getRandomString(),
oid: user.oid,
AlternateIds: "",
BirthDate: "13/06/1941",
PublicServiceNumber: "0111019P",
Expand Down
20 changes: 16 additions & 4 deletions mygovid-mock-service/src/routes/static/mock-login.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
/>
<input id="lastName" type="hidden" name="lastName" value="" readonly />
<input id="email" type="hidden" name="email" value="" readonly />
<input id="oid" type="hidden" name="oid" value="" readonly />
<input id="sub" type="hidden" name="sub" value="" readonly />
<input
id="redirect_url"
type="hidden"
Expand Down Expand Up @@ -209,12 +211,16 @@
users: [
{
user_name: "Peter Parker",
govid_email: "peter.parker@mail.ie"
govid_email: "peter.parker@mail.ie",
oid: "4cfa878a4fd9892a64ac",
sub: "932d94fc69be147f6fcb",
},
{
user_name: "Tony Stark",
govid_email: "tony.stark@gov.ie",
is_public_servant: true
govid_email: "tony.stark@mail.ie",
oid: "71848ec91433bc4222d0",
sub: "7ffe40ff7d558de01c54",
is_public_servant: true,
},
]
}
Expand All @@ -235,24 +241,30 @@
(user) => user.govid_email === e.target.value,
);

let firstName, lastName, email;
let firstName, lastName, email, oid, sub;

if (user) {
const nameSplit = user.user_name.split(" ");
firstName = nameSplit.at(0);
lastName = nameSplit.slice(1).join(" ");
email = user.govid_email;
isCurrentUserSet = true;
oid = user.oid;
sub = user.sub;
} else {
firstName = faker.person.firstName();
lastName = faker.person.lastName();
email = `${firstName.toLowerCase()}.${lastName.toLowerCase()}@mail.ie`;
isCurrentUserSet = false;
oid = crypto.randomBytes(20).toString("hex");
sub = crypto.randomBytes(20).toString("hex");
}

document.querySelector("#firstName").value = firstName;
document.querySelector("#lastName").value = lastName;
document.querySelector("#email").value = email;
document.querySelector("#oid").value = oid;
document.querySelector("#sub").value = sub;
document.querySelector("#submit_btn").innerHTML =
`<div style="margin-top: 5px">Login ${firstName} ${lastName} <span class="icon-signin_id_logo"></span></div>`;
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"typescript": "^5.0.0"
},
"engines": {
"node": "^20.9.0",
"node": "^20.10.0",
"pnpm": "^9.0.0"
},
"pnpm": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@
"id": "login_webhook",
"name": "User log in",
"events": [
"PostRegister",
"PostSignIn"
"User.Created",
"User.Deleted",
"User.Data.Updated",
"User.SuspensionStatus.Updated"
],
"config": {
"url": "<SEEDER_WEBHOOK_LOGIN_URL>"
Expand All @@ -111,4 +113,4 @@
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@
"id": "login-webhook",
"name": "User log in",
"events": [
"PostRegister",
"PostSignIn"
"User.Created",
"User.Deleted",
"User.Data.Updated",
"User.SuspensionStatus.Updated"
],
"config": {
"url": "http://localhost:8003/user-login-wh"
Expand All @@ -190,4 +192,4 @@
}
]
}
}
}
8 changes: 5 additions & 3 deletions packages/cli/src/commands/database/ogcio/ogcio-seeder.json
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@
"id": "login-webhook",
"name": "User log in",
"events": [
"PostRegister",
"PostSignIn"
"User.Created",
"User.Deleted",
"User.Data.Updated",
"User.SuspensionStatus.Updated"
],
"config": {
"url": "<SEEDER_WEBHOOK_LOGIN_URL>"
Expand All @@ -190,4 +192,4 @@
}
]
}
}
}