forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsep10Controller.js
More file actions
120 lines (110 loc) · 3.77 KB
/
Copy pathsep10Controller.js
File metadata and controls
120 lines (110 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// controllers/stellar/sep10Controller.js
//
// "Sign in with Stellar" (SEP-10). Wire format matches the merged frontend
// (dnb-frontend #164 / hooks/useStellarAuth.js):
// GET /api/auth/stellar/challenge?account=G… → { transaction, networkPassphrase }
// POST /api/auth/stellar/verify { transaction } → { token, user } (linked wallet)
// → 200 { registered:false, accountProven } (unlinked)
// When the feature is unconfigured, both return 503 so the frontend disables the
// button gracefully instead of erroring.
import User from "../../models/User.js";
import {
buildChallenge,
verifyChallenge,
isSep10Configured,
} from "../../services/stellar/sep10Service.js";
import {
createSessionAndTokens,
shapeAuthUser,
} from "../authController.js";
import { catchAsync } from "../../middlewares/errorHandler.js";
import logger from "../../config/logger.js";
/**
* GET /api/auth/stellar/challenge?account=G…
* Returns a server-signed SEP-10 challenge for the wallet to sign.
*/
export const getStellarChallenge = catchAsync(async (req, res) => {
if (!isSep10Configured()) {
return res.status(503).json({
success: false,
message:
"Sign in with Stellar is not available yet. Please use email login.",
});
}
const account = req.query.account;
try {
const { transaction, network_passphrase } = buildChallenge(account);
return res.status(200).json({
success: true,
transaction,
// camelCase for the frontend; snake_case for SEP-10-conformant wallets.
networkPassphrase: network_passphrase,
network_passphrase,
});
} catch (err) {
if (err.code === "INVALID_ACCOUNT") {
return res.status(400).json({ success: false, message: err.message });
}
throw err;
}
});
/**
* POST /api/auth/stellar/verify { transaction: <signed XDR> }
* Verifies the signed challenge; issues a platform JWT for a linked wallet or
* reports the proven-but-unregistered account without creating a user.
*/
export const verifyStellarChallenge = catchAsync(async (req, res) => {
if (!isSep10Configured()) {
return res.status(503).json({
success: false,
message:
"Sign in with Stellar is not available yet. Please use email login.",
});
}
const signedXdr = req.body?.transaction;
let provenAccount;
try {
provenAccount = await verifyChallenge(signedXdr);
} catch (err) {
if (err.code === "CHALLENGE_REPLAYED") {
return res.status(401).json({ success: false, message: err.message });
}
// INVALID_CHALLENGE covers wrong sig, expired time bounds, tampered domain.
// 401 + the SDK message lets the frontend detect expiry and retry once.
return res.status(401).json({
success: false,
message: err.message || "Challenge verification failed.",
});
}
const user = await User.findOne({
"stellarWallet.publicKey": provenAccount,
}).select("-password");
// Ownership is cryptographically proven but no account is linked to it yet.
// Do NOT create a user here — let the frontend route to signup/link.
if (!user) {
logger.info(
`SEP-10: proven unregistered wallet ${provenAccount.slice(0, 6)}…`
);
return res.status(200).json({
success: true,
registered: false,
accountProven: provenAccount,
});
}
// Linked wallet → issue the exact same session/JWT as email login.
const { accessToken, refreshToken } = await createSessionAndTokens(
user,
req,
res
);
logger.info(
`✅ SEP-10 login: ${user.email} via wallet ${provenAccount.slice(0, 6)}…`
);
return res.status(200).json({
success: true,
accessToken,
refreshToken,
token: accessToken, // legacy field the frontend reads as `token`
user: shapeAuthUser(user),
});
});