-
Notifications
You must be signed in to change notification settings - Fork 521
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: add support for oauth in public apis #1049
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cca05a9
chore: add previewIam domain
tiwarishubham635 013178d
chore: add authStrategy
tiwarishubham635 a9826ba
chore: add no auth client credential provider
tiwarishubham635 4916098
chore: add token auth strategy and client credential provider
tiwarishubham635 54b5178
chore: modify client for oauth
tiwarishubham635 28d9d1d
chore: add orgs credential provider
tiwarishubham635 c62e524
chore: add orgs credential provider
tiwarishubham635 3f8023d
chore: run prettier
tiwarishubham635 086984f
chore: added getters and error messages
tiwarishubham635 7fe5675
chore: added authStrategy tests
tiwarishubham635 07d1651
chore: run prettier
tiwarishubham635 e01b796
chore: fix test describe
tiwarishubham635 4968d9f
chore: add credential provider tests
tiwarishubham635 ffc42aa
chore: add token manager tests
tiwarishubham635 1297e26
chore: shifting tests to other PR
tiwarishubham635 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export default abstract class AuthStrategy { | ||
private authType: string; | ||
protected constructor(authType: string) { | ||
this.authType = authType; | ||
} | ||
getAuthType(): string { | ||
return this.authType; | ||
} | ||
abstract getAuthString(): Promise<string>; | ||
abstract requiresAuthentication(): boolean; | ||
} |
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,23 @@ | ||
import AuthStrategy from "./AuthStrategy"; | ||
|
||
export default class BasicAuthStrategy extends AuthStrategy { | ||
private username: string; | ||
private password: string; | ||
|
||
constructor(username: string, password: string) { | ||
super("basic"); | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
getAuthString(): Promise<string> { | ||
const auth = Buffer.from(this.username + ":" + this.password).toString( | ||
"base64" | ||
); | ||
return Promise.resolve(`Basic ${auth}`); | ||
} | ||
|
||
requiresAuthentication(): boolean { | ||
return true; | ||
} | ||
} |
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,15 @@ | ||
import AuthStrategy from "./AuthStrategy"; | ||
|
||
export default class NoAuthStrategy extends AuthStrategy { | ||
constructor() { | ||
super("noauth"); | ||
} | ||
|
||
getAuthString(): Promise<string> { | ||
return Promise.resolve(""); | ||
} | ||
|
||
requiresAuthentication(): boolean { | ||
return false; | ||
} | ||
} |
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,67 @@ | ||
import AuthStrategy from "./AuthStrategy"; | ||
import TokenManager from "../http/bearer_token/TokenManager"; | ||
import jwt, { JwtPayload } from "jsonwebtoken"; | ||
|
||
export default class TokenAuthStrategy extends AuthStrategy { | ||
private token: string; | ||
private tokenManager: TokenManager; | ||
|
||
constructor(tokenManager: TokenManager) { | ||
super("token"); | ||
this.token = ""; | ||
this.tokenManager = tokenManager; | ||
} | ||
|
||
async getAuthString(): Promise<string> { | ||
return this.fetchToken() | ||
.then((token) => { | ||
this.token = token; | ||
return `Bearer ${this.token}`; | ||
}) | ||
.catch((error) => { | ||
throw new Error(`Failed to fetch access token: ${error.message}`); | ||
}); | ||
} | ||
|
||
requiresAuthentication(): boolean { | ||
return true; | ||
} | ||
|
||
async fetchToken(): Promise<string> { | ||
if ( | ||
this.token == null || | ||
this.token.length === 0 || | ||
this.isTokenExpired(this.token) | ||
) { | ||
return this.tokenManager.fetchToken(); | ||
} | ||
return Promise.resolve(this.token); | ||
} | ||
|
||
/** | ||
* Function to check if the token is expired with a buffer of 30 seconds. | ||
* @param token - The JWT token as a string. | ||
* @returns Boolean indicating if the token is expired. | ||
*/ | ||
isTokenExpired(token: string): boolean { | ||
try { | ||
// Decode the token without verifying the signature, as we only want to read the expiration for this check | ||
const decoded = jwt.decode(token) as JwtPayload; | ||
|
||
if (!decoded || !decoded.exp) { | ||
// If the token doesn't have an expiration, consider it expired | ||
return true; | ||
} | ||
|
||
const expiresAt = decoded.exp * 1000; | ||
const bufferMilliseconds = 30 * 1000; | ||
const bufferExpiresAt = expiresAt - bufferMilliseconds; | ||
|
||
// Return true if the current time is after the expiration time with buffer | ||
return Date.now() > bufferExpiresAt; | ||
} catch (error) { | ||
// If there's an error decoding the token, consider it expired | ||
return true; | ||
} | ||
} | ||
} |
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,66 @@ | ||
import CredentialProvider from "./CredentialProvider"; | ||
import TokenManager from "../http/bearer_token/TokenManager"; | ||
import AuthStrategy from "../auth_strategy/AuthStrategy"; | ||
import ApiTokenManager from "../http/bearer_token/ApiTokenManager"; | ||
import TokenAuthStrategy from "../auth_strategy/TokenAuthStrategy"; | ||
|
||
class ClientCredentialProvider extends CredentialProvider { | ||
grantType: string; | ||
clientId: string; | ||
clientSecret: string; | ||
tokenManager: TokenManager | null; | ||
|
||
constructor() { | ||
super("client-credentials"); | ||
this.grantType = "client_credentials"; | ||
this.clientId = ""; | ||
this.clientSecret = ""; | ||
this.tokenManager = null; | ||
} | ||
|
||
public toAuthStrategy(): AuthStrategy { | ||
if (this.tokenManager == null) { | ||
this.tokenManager = new ApiTokenManager({ | ||
grantType: this.grantType, | ||
clientId: this.clientId, | ||
clientSecret: this.clientSecret, | ||
}); | ||
} | ||
return new TokenAuthStrategy(this.tokenManager); | ||
} | ||
} | ||
|
||
namespace ClientCredentialProvider { | ||
export class ClientCredentialProviderBuilder { | ||
private readonly instance: ClientCredentialProvider; | ||
|
||
constructor() { | ||
this.instance = new ClientCredentialProvider(); | ||
} | ||
|
||
public setClientId(clientId: string): ClientCredentialProviderBuilder { | ||
this.instance.clientId = clientId; | ||
return this; | ||
} | ||
|
||
public setClientSecret( | ||
clientSecret: string | ||
): ClientCredentialProviderBuilder { | ||
this.instance.clientSecret = clientSecret; | ||
return this; | ||
} | ||
|
||
public setTokenManager( | ||
tokenManager: TokenManager | ||
): ClientCredentialProviderBuilder { | ||
this.instance.tokenManager = tokenManager; | ||
return this; | ||
} | ||
|
||
public build(): ClientCredentialProvider { | ||
return this.instance; | ||
} | ||
} | ||
} | ||
|
||
export = ClientCredentialProvider; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need another Credential provider for Orgs API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added Orgs credential provider here