-
Notifications
You must be signed in to change notification settings - Fork 166
feat: add service account support #275
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
Merged
lajohn4747
merged 23 commits into
master
from
johnla-multi-563-add-service-account-support-to-nodejs-sdk
Jul 9, 2026
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
75bde57
Add Service Account support
lajohn4747 32d813c
Clean up some comments
lajohn4747 cb0e3c6
Fix lint and remove debug conditional
lajohn4747 b398674
Fix lint test
lajohn4747 70a023e
add project id along with token
lajohn4747 5f17a36
FIx declaration for types
lajohn4747 25308a8
Fix TypeScript export to properly expose init method
lajohn4747 16d3654
Revert
lajohn4747 bac8656
Update documentation
lajohn4747 2bcc48f
Add more tests
lajohn4747 a4d41e0
Remove comments
lajohn4747 538b77c
Fix lint
lajohn4747 4a4c078
Only warn deprecation on client creation
lajohn4747 279889e
Clean up nits from copilot
lajohn4747 eb1bf5c
Fix signatures
lajohn4747 dce332b
Run lint
lajohn4747 0e591a6
Revert test change
lajohn4747 8c7e526
Add more tests
lajohn4747 89329a4
Greptile reviews
lajohn4747 3188c39
Fix log spam for multiple clients
lajohn4747 66ebec2
Add deprecation warnings for api_secret/api_key using process.emitWar…
lajohn4747 5777117
Revert back to warning everytime a client is initialized
lajohn4747 0bb7cd1
Fix lint
lajohn4747 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,81 @@ | ||
| /** | ||
| * Service account credentials for server-to-server authentication. | ||
| * | ||
| * Service account authentication is the recommended method for server-side | ||
| * integrations. It provides better security than API secrets by using | ||
| * unique username/secret pairs instead of a single shared secret. | ||
| * | ||
| * @example | ||
| * const { ServiceAccountCredentials } = require('mixpanel/lib/credentials'); | ||
| * | ||
| * const credentials = new ServiceAccountCredentials( | ||
| * 'your-service-account-username', | ||
| * 'your-service-account-secret', | ||
| * '123456' | ||
| * ); | ||
| * const mixpanel = Mixpanel.init('YOUR_TOKEN', { credentials }); | ||
| */ | ||
| class ServiceAccountCredentials { | ||
| /** | ||
| * Create service account credentials. | ||
| * | ||
| * @param {string} username - Service account username | ||
| * @param {string} secret - Service account secret | ||
| * @param {string} project_id - Mixpanel project ID | ||
| * @throws {TypeError} If any parameter is not a string, empty, or whitespace-only | ||
| */ | ||
| constructor(username, secret, project_id) { | ||
| if (typeof username !== "string") { | ||
| throw new TypeError("Service account username must be a string"); | ||
| } | ||
| if (typeof secret !== "string") { | ||
| throw new TypeError("Service account secret must be a string"); | ||
| } | ||
| if (typeof project_id !== "string") { | ||
| throw new TypeError("Service account project_id must be a string"); | ||
| } | ||
|
|
||
| const trimmedUsername = username.trim(); | ||
| const trimmedSecret = secret.trim(); | ||
| const trimmedProjectId = project_id.trim(); | ||
|
|
||
| if (!trimmedUsername) { | ||
| throw new TypeError("Service account username cannot be empty"); | ||
| } | ||
| if (!trimmedSecret) { | ||
| throw new TypeError("Service account secret cannot be empty"); | ||
| } | ||
| if (!trimmedProjectId) { | ||
| throw new TypeError("Service account project_id cannot be empty"); | ||
| } | ||
|
|
||
| this.username = trimmedUsername; | ||
| this.secret = trimmedSecret; | ||
| this.project_id = trimmedProjectId; | ||
|
|
||
| // Cache the base64-encoded auth to avoid recomputing on every request | ||
| this._cachedAuth = Buffer.from(this.username + ":" + this.secret).toString( | ||
| "base64", | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Convert credentials to HTTP Basic Auth format. | ||
| * | ||
| * @returns {string} Base64-encoded username:secret for Authorization header | ||
| */ | ||
| toHttpBasicAuth() { | ||
| return this._cachedAuth; | ||
| } | ||
|
|
||
| /** | ||
| * String representation of credentials (masks secret). | ||
| * | ||
| * @returns {string} | ||
| */ | ||
| toString() { | ||
| return `ServiceAccountCredentials(username=${this.username}, project_id=${this.project_id}, secret=***)`; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { ServiceAccountCredentials }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.