Skip to content
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

Add Support for Customizable OpenID Profile Fields via Environment Variable #4362

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ OPENID_REQUIRED_ROLE_PARAMETER_PATH=

OPENID_BUTTON_LABEL=
OPENID_IMAGE_URL=
OPENID_CUSTOM_DATA=

# LDAP
LDAP_URL=
Expand Down
4 changes: 4 additions & 0 deletions api/models/schema/userSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ const userSchema = mongoose.Schema(
unique: true,
sparse: true,
},
customOpenIdData: {
type: Map,
of: String
},
ldapId: {
type: String,
unique: true,
Expand Down
32 changes: 32 additions & 0 deletions api/strategies/openidStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,33 @@ function convertToUsername(input, defaultValue = '') {
return defaultValue;
}

async function mapCustomOpenIdData(accessToken, customOpenIdFields) {
const customData = {};
const fieldsQueryURL = `https://graph.microsoft.com/v1.0/me?$select=${customOpenIdFields.join(',')}`;
Copy link
Owner

@danny-avila danny-avila Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unique to Microsoft OpenID services. The PR notes and documentation write-up make it seem like it could be applicable to many different OIDC providers but I don't think this is usually the case.

Different OIDC providers may have different ways of accessing additional user information:

  • Google has its People API
  • Auth0 has its Management API
  • Okta has its User API
  • etc.

1 or more of these approaches would be fine for me to merge:

  1. Make it explicitly Microsoft-specific and name it accordingly (e.g., mapMicrosoftCustomOpenIdData)

  2. Make it provider-agnostic by:

   async function mapCustomOpenIdData(accessToken, customOpenIdFields, providerConfig) {
     const { userInfoEndpoint, fieldMapping } = providerConfig;
     // Use configurable endpoint and field mapping logic
     // ...
   }
  1. Create an abstraction layer with provider-specific implementations:
const PROVIDER_MAPPERS = {
  microsoft: MicrosoftDataMapper,
  google: GoogleDataMapper,
  // Add more providers as needed
};

class OpenIdDataMapper {
  static getMapper(provider) {
    const MapperClass = PROVIDER_MAPPERS[provider];
    if (!MapperClass) {
      throw new Error(`No mapper found for provider: ${provider}`);
    }
    return new MapperClass();
  }
}

// Example implementations
class MicrosoftDataMapper {
  async mapCustomData(accessToken, customOpenIdFields) {
    const fieldsQueryURL = `https://graph.microsoft.com/v1.0/me?$select=${customOpenIdFields.join(',')}`;
    // Microsoft-specific implementation...
  }
}

class GoogleDataMapper {
  async mapCustomData(accessToken, customOpenIdFields) {
    const fieldsQueryURL = `https://people.googleapis.com/v1/people/me...`;
    // Google-specific implementation...
  }
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer the 3rd approach, since that seems like it would be the cleanest and easily allow implementations for Okta, Microsoft, etc.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late response. By the time I created the pull request I wasn't aware that there were other OIDC providers. You are totally right. I'll update the pull request with your comments. I also prefer the 3rd approach because it is reusable, but I have no way to test it :(. Do you have any ideas how to approach that problem ?


const response = await fetch(fieldsQueryURL, {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
});


if (response.ok) {
const customOpenIdFieldsResult = await response.json();

// Extract relevant fields from the response
customOpenIdFields.forEach(field => {
if (customOpenIdFieldsResult[field]) {
customData[field] = customOpenIdFieldsResult[field];
}
});
}
logger.debug('[openidStrategy] map custom openId data', { fieldsQueryURL, customData });
return customData;
}

async function setupOpenId() {
try {
if (process.env.PROXY) {
Expand Down Expand Up @@ -202,6 +229,11 @@ async function setupOpenId() {
}
}

const customOpenIdFields = process.env.OPENID_CUSTOM_DATA ? process.env.OPENID_CUSTOM_DATA.split(" ") : [];
if (customOpenIdFields.length > 0) {
user.customOpenIdData = new Map(Object.entries(await mapCustomOpenIdData(tokenset.access_token, customOpenIdFields)));
}

user = await updateUser(user._id, user);

logger.info(
Expand Down
Loading