Skip to content

Commit

Permalink
feat: running but stil dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
agus-darmawan committed Dec 9, 2024
1 parent a42b0bd commit f5b1aae
Show file tree
Hide file tree
Showing 4 changed files with 4,460 additions and 1,575 deletions.
1 change: 1 addition & 0 deletions app/controllers/auth_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class AuthController {
}

async register({ request, response }: HttpContext) {
console.log('Registering...')
const data = await vine
.compile(AuthValidator.registerSchema)
.validate(request.all(), { messagesProvider })
Expand Down
24 changes: 21 additions & 3 deletions app/controllers/oauth_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ export default class OauthController {
const oauthClient = await OauthClient.findBy('client_id', clientId)
if (!oauthClient) return null

console.log('Oauth client found:', oauthClient.client_id)

const allowedScopes = oauthClient.allowed_scopes.split(',').map((s) => s.trim())
const validScopes = this.validateScopes(scopes, allowedScopes)
if (!validScopes) return null
console.log('Valid scopes:', validScopes)

if (!oauthClient.redirect_uri.startsWith('http')) {
return null
}
const allowedUri = new URL(oauthClient.redirect_uri)
const requestedUri = new URL(redirectUri)
if (
Expand All @@ -32,6 +38,7 @@ export default class OauthController {
) {
return null
}
console.log('Valid redirect_uri:', redirectUri)
return oauthClient
}

Expand All @@ -44,42 +51,52 @@ export default class OauthController {
state,
nonce,
} = request.qs()
console.log('Authorize request:', request.qs())

if (!clientId || !redirectUri || !scope || !state) {
console.log('Missing required parameters.')
return response.unprocessableEntity({
success: false,
message: 'Missing required parameters.',
})
}

if (responseType !== 'code') {
console.log("Invalid response_type. Only 'code' is allowed.")
return response.unauthorized({
success: false,
message: 'Invalid response_type. Only "code" is allowed.',
})
}

const oauthClient = await this.validateOauthClient(clientId, scope, redirectUri)
console.log('Oauth client:', oauthClient)

if (!oauthClient) {
console.log('Invalid client_id, scope, or redirect_uri.')
return response.unauthorized({
success: false,
message: 'Invalid client_id, scope, or redirect_uri.',
})
}

console.log('Valid client_id, scope, and redirect_uri.')

try {
const data = await vine
.compile(AuthValidator.loginSchema)
.validate(request.all(), { messagesProvider })

const user = await User.verifyCredentials(data.email, data.password)
console.log('User found:', user.email)
if (!user) {
console.log('User not found:', data.email)
return response.unauthorized({
success: false,
message: 'Invalid email or password.',
})
}

console.log('User found:', user.email)
const authCode = await AuthCode.create({
code: uuidv4(),
clientId: oauthClient.client_id,
Expand All @@ -91,12 +108,13 @@ export default class OauthController {
expiresAt: DateTime.utc().plus({ minutes: 5 }),
})

console.log('Authorization code generated:', authCode.code)

return response.ok({
success: true,
message: 'Authorization code generated successfully.',
data: {
code: authCode.code,
state: state,
uri: `${redirectUri}?code=${authCode.code}&state=${state}`,
},
})
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/1733583420363_create_auth_codes_table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export default class extends BaseSchema {
table.string('client_id').unsigned().references('oauth_clients.client_id')
table.integer('user_id').unsigned().references('users.id')
table.string('nonce').nullable()
table.string('scopes').notNullable().defaultTo('openid profile email')
table.string('state').nullable()
table.string('scopes').notNullable().defaultTo('openid,profile,email')
table.string('redirect_uri').notNullable()
table.timestamp('expires_at').notNullable()
table.timestamp('created_at')
Expand Down
Loading

0 comments on commit f5b1aae

Please sign in to comment.