-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: globalping_client_credentials grant (#7)
- Loading branch information
1 parent
ab08c59
commit 3c14f9c
Showing
23 changed files
with
174 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
AbstractGrantType, | ||
InvalidArgumentError, | ||
InvalidGrantError, | ||
Request, | ||
} from '@node-oauth/oauth2-server'; | ||
import { ClientCredentialsUser, ClientWithCredentials, GrantTypeOptions, User } from './types.js'; | ||
import OAuthModel from './model.js'; | ||
|
||
export default class GPClientCredentials extends AbstractGrantType { | ||
model: OAuthModel; | ||
|
||
constructor (options: GrantTypeOptions = {}) { | ||
if (!options.model) { | ||
throw new InvalidArgumentError('Missing parameter: `model`'); | ||
} | ||
|
||
if (!options.model.getUserFromClient) { | ||
throw new InvalidArgumentError('Invalid argument: model does not implement `getUserFromClient()`'); | ||
} | ||
|
||
if (!options.model.saveToken) { | ||
throw new InvalidArgumentError('Invalid argument: model does not implement `saveToken()`'); | ||
} | ||
|
||
super(options); | ||
this.model = options.model; | ||
} | ||
|
||
async handle (request: Request, client: ClientWithCredentials) { | ||
if (!request) { | ||
throw new InvalidArgumentError('Missing parameter: `request`'); | ||
} | ||
|
||
if (!client) { | ||
throw new InvalidArgumentError('Missing parameter: `client`'); | ||
} | ||
|
||
const scope = this.getScope(request); | ||
const user = await this.getUserFromClient(client); | ||
|
||
return this.saveToken(user, client, scope); | ||
} | ||
|
||
async getUserFromClient (client: ClientWithCredentials) { | ||
const user = await this.model.getUserFromClient(client); | ||
|
||
if (!user) { | ||
throw new InvalidGrantError('Invalid grant: user credentials are invalid'); | ||
} | ||
|
||
return user; | ||
} | ||
|
||
async saveToken (user: ClientCredentialsUser, client: ClientWithCredentials, requestedScope: string[]) { | ||
const validatedScope = await this.validateScope(user, client, requestedScope) as string[]; | ||
const accessToken = await this.generateAccessToken(client, user, validatedScope); | ||
const refreshToken = await this.generateRefreshToken(client, user, validatedScope); | ||
const accessTokenExpiresAt = this.getAccessTokenExpiresAt(); | ||
const refreshTokenExpiresAt = this.getRefreshTokenExpiresAt(); | ||
|
||
const token = { | ||
accessToken, | ||
accessTokenExpiresAt, | ||
refreshToken, | ||
refreshTokenExpiresAt, | ||
scope: validatedScope, | ||
}; | ||
|
||
return this.model.saveToken(token, client, user as unknown as User); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.