Skip to content

Commit 4ae262d

Browse files
authored
fix: verify steam credentials (#365)
1 parent 3a50df1 commit 4ae262d

File tree

1 file changed

+39
-13
lines changed

1 file changed

+39
-13
lines changed

src/runtime/server/lib/oauth/steam.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,58 @@ export function defineOAuthSteamEventHandler({ config, onSuccess, onError }: OAu
3838
return handleMissingConfiguration(event, 'steam', ['apiKey'], onError)
3939
}
4040

41+
const url = getRequestURL(event)
42+
4143
if (!query['openid.claimed_id']) {
4244
const redirectURL = config.redirectURL || getRequestURL(event).href
4345
const steamOpenIdParams = {
4446
'openid.ns': 'http://specs.openid.net/auth/2.0',
4547
'openid.mode': 'checkid_setup',
4648
'openid.return_to': redirectURL,
49+
'openid.realm': `${url.protocol}//${url.hostname}`,
4750
'openid.identity': 'http://specs.openid.net/auth/2.0/identifier_select',
4851
'openid.claimed_id': 'http://specs.openid.net/auth/2.0/identifier_select',
4952
}
5053
// Redirect to Steam Oauth page
5154
return sendRedirect(event, withQuery(config.authorizationURL as string, steamOpenIdParams))
5255
}
5356

54-
const openIdCheck = {
55-
ns: 'http://specs.openid.net/auth/2.0',
56-
claimed_id: 'https://steamcommunity.com/openid/id/',
57-
identity: 'https://steamcommunity.com/openid/id/',
57+
if (!query['openid.signed']
58+
|| !query['openid.sig']
59+
) {
60+
const error = createError({
61+
statusCode: 400,
62+
message: 'Steam login failed: Incomplete query.',
63+
})
64+
if (!onError) throw error
65+
return onError(event, error)
5866
}
5967

60-
const idRegex = /^https?:\/\/steamcommunity\.com\/openid\/id\/(\d+)$/
61-
const steamIdCheck = idRegex.exec(query['openid.claimed_id'])
68+
const openIdCheck: Record<string, string> = {
69+
'openid.ns': 'http://specs.openid.net/auth/2.0',
70+
'openid.mode': 'check_authentication',
71+
'openid.signed': query['openid.signed'],
72+
'openid.sig': query['openid.sig'],
73+
}
6274

63-
if (
64-
query['openid.op_endpoint'] !== config.authorizationURL
65-
|| !steamIdCheck
66-
|| query['openid.ns'] !== openIdCheck.ns
67-
|| !query['openid.claimed_id']?.startsWith(openIdCheck.claimed_id)
68-
|| !query['openid.identity']?.startsWith(openIdCheck.identity)
69-
) {
75+
for (const signed of query['openid.signed'].split(',')) {
76+
if (!query[`openid.${signed}`]) {
77+
const error = createError({
78+
statusCode: 400,
79+
message: 'Steam login failed: Incomplete query.',
80+
})
81+
if (!onError) throw error
82+
return onError(event, error)
83+
}
84+
openIdCheck[`openid.${signed}`] = query[`openid.${signed}`]
85+
}
86+
87+
const auth_validation: string = await $fetch(withQuery(config?.authorizationURL as string, openIdCheck))
88+
89+
const validRegex = /is_valid:true/
90+
const valid = validRegex.test(auth_validation)
91+
92+
if (!valid) {
7093
const error = createError({
7194
statusCode: 401,
7295
message: 'Steam login failed: Claimed identity is invalid.',
@@ -75,6 +98,9 @@ export function defineOAuthSteamEventHandler({ config, onSuccess, onError }: OAu
7598
return onError(event, error)
7699
}
77100

101+
const idRegex = /^https?:\/\/steamcommunity\.com\/openid\/id\/(\d+)$/
102+
const steamIdCheck = idRegex.exec(query['openid.claimed_id'])
103+
78104
const steamId = steamIdCheck[1]
79105

80106
// TODO: improve typing

0 commit comments

Comments
 (0)