@eaccess/auth 0.1.19 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -55,6 +55,7 @@ interface AuthConfig {
55
55
  google?: GoogleProviderConfig;
56
56
  azure?: AzureProviderConfig;
57
57
  };
58
+ githubUserAgent?: string;
58
59
  twoFactor?: {
59
60
  enabled?: boolean;
60
61
  requireForOAuth?: boolean;
@@ -1167,7 +1168,7 @@ declare abstract class BaseOAuthProvider implements OAuthProvider {
1167
1168
  protected processOAuthLogin(userData: OAuthUserData, req: Request): Promise<OAuthCallbackResult>;
1168
1169
  protected abstract getProviderName(): string;
1169
1170
  protected exchangeCodeForToken(code: string, tokenUrl: string): Promise<string>;
1170
- protected fetchUserFromAPI(accessToken: string, apiUrl: string): Promise<any>;
1171
+ protected fetchUserFromAPI(accessToken: string, apiUrl: string, headers?: Record<string, string>): Promise<any>;
1171
1172
  }
1172
1173
 
1173
1174
  declare class GitHubProvider extends BaseOAuthProvider {
package/dist/index.d.ts CHANGED
@@ -55,6 +55,7 @@ interface AuthConfig {
55
55
  google?: GoogleProviderConfig;
56
56
  azure?: AzureProviderConfig;
57
57
  };
58
+ githubUserAgent?: string;
58
59
  twoFactor?: {
59
60
  enabled?: boolean;
60
61
  requireForOAuth?: boolean;
@@ -1167,7 +1168,7 @@ declare abstract class BaseOAuthProvider implements OAuthProvider {
1167
1168
  protected processOAuthLogin(userData: OAuthUserData, req: Request): Promise<OAuthCallbackResult>;
1168
1169
  protected abstract getProviderName(): string;
1169
1170
  protected exchangeCodeForToken(code: string, tokenUrl: string): Promise<string>;
1170
- protected fetchUserFromAPI(accessToken: string, apiUrl: string): Promise<any>;
1171
+ protected fetchUserFromAPI(accessToken: string, apiUrl: string, headers?: Record<string, string>): Promise<any>;
1171
1172
  }
1172
1173
 
1173
1174
  declare class GitHubProvider extends BaseOAuthProvider {
package/dist/index.js CHANGED
@@ -696,11 +696,12 @@ var BaseOAuthProvider = class {
696
696
  }
697
697
  return data.access_token;
698
698
  }
699
- async fetchUserFromAPI(accessToken, apiUrl) {
699
+ async fetchUserFromAPI(accessToken, apiUrl, headers = {}) {
700
700
  const response = await fetch(apiUrl, {
701
701
  headers: {
702
702
  Authorization: `Bearer ${accessToken}`,
703
- Accept: "application/json"
703
+ Accept: "application/json",
704
+ ...headers
704
705
  }
705
706
  });
706
707
  if (!response.ok) {
@@ -731,14 +732,24 @@ var GitHubProvider = class extends BaseOAuthProvider {
731
732
  throw new Error("No authorization code provided");
732
733
  }
733
734
  const accessToken = await this.exchangeCodeForToken(code, "https://github.com/login/oauth/access_token");
734
- const [user, emails] = await Promise.all([this.fetchUserFromAPI(accessToken, "https://api.github.com/user"), this.fetchUserFromAPI(accessToken, "https://api.github.com/user/emails")]);
735
- const primaryEmail = Array.isArray(emails) ? emails.find((email) => email.primary)?.email : null;
736
- if (!primaryEmail) {
737
- throw new Error("No primary email found in GitHub account");
735
+ const apiHeaders = {
736
+ Accept: "application/vnd.github+json",
737
+ "User-Agent": this.authConfig.githubUserAgent || "EasyAccess",
738
+ "X-GitHub-Api-Version": "2022-11-28"
739
+ };
740
+ const [user, emails] = await Promise.all([
741
+ this.fetchUserFromAPI(accessToken, "https://api.github.com/user", apiHeaders),
742
+ this.fetchUserFromAPI(accessToken, "https://api.github.com/user/emails", apiHeaders)
743
+ ]);
744
+ const verifiedEmails = Array.isArray(emails) ? emails.filter((email) => email.verified) : [];
745
+ const primaryEmail = verifiedEmails.find((email) => email.primary)?.email;
746
+ const fallbackEmail = primaryEmail || verifiedEmails[0]?.email;
747
+ if (!fallbackEmail) {
748
+ throw new Error("No verified email found in GitHub account");
738
749
  }
739
750
  return {
740
751
  id: user.id.toString(),
741
- email: primaryEmail,
752
+ email: fallbackEmail,
742
753
  username: user.login,
743
754
  name: user.name || user.login,
744
755
  avatar: user.avatar_url