Skip to content

Expose MongoDB collections via read-only OAuth2Server getter for granular access #27

@alloalexandre

Description

@alloalexandre

Is your feature request related to a problem? Please describe.

Currently, the OAuth2Server package encapsulates its MongoDB collections (AccessTokens, RefreshTokens, AuthCodes, Clients) within the OAuthMeteorModel, making it impossible for developers to perform custom queries or create indexes directly on these collections. This is frustrating when implementing features like admin dashboards (e.g., listing expired tokens) or custom cleanup logic (e.g., removing stale authorization codes), as developers must rely solely on the model's predefined methods, which may not cover all use cases.

Describe the solution you'd like

Add a read-only getter (collections) to the OAuth2Server class that exposes the underlying Mongo.Collection instances (AccessTokens, RefreshTokens, AuthCodes, Clients). The getter should return a frozen object (using Object.freeze) to prevent mutations, ensuring OAuth2 compliance by forcing write operations through the model's methods. This allows developers to perform custom queries or indexing while maintaining encapsulation.

Example Usage:

import { OAuth2Server } from 'meteor/leaonline:oauth2-server';

const server = new OAuth2Server({ /* config */ });
const expiredTokens = server.collections.AccessTokens.find({
  accessTokenExpiresAt: { $lt: new Date() }
}).fetch();
console.log(expiredTokens); // Array of expired access tokens
// Mutation attempt fails:
server.collections.AccessTokens = {}; // TypeError: Cannot assign to read only property

Describe alternatives you've considered

  1. Using Mongo.getCollection from the MongoDB driver: This would expose raw MongoDB collections, bypassing Meteor's Mongo.Collection abstraction. It risks breaking Meteor's reactivity, security rules, and OAuth2 compliance, and is less user-friendly for Meteor developers.
  2. Adding specific query methods to OAuthMeteorModel: Instead of exposing collections, we could add methods like findAccessTokens or createIndex. However, this would require anticipating all possible use cases, leading to a bloated API and maintenance overhead.
  3. No exposure, rely on existing methods: Continuing to restrict access to collections forces developers to work around limitations using less efficient or hacky solutions (e.g., duplicating data in custom collections), which is suboptimal.

The proposed getter approach is the most flexible, secure, and Meteor-aligned solution, as it leverages existing Mongo.Collection instances without exposing low-level driver APIs.

Additional context

  • Security: The getter uses Object.freeze to ensure read-only access, preventing accidental mutations that could violate OAuth2 standards (e.g., RFC 6749). Write operations must still use OAuthMeteorModel methods like saveToken or revokeAuthorizationCode.
  • Meteor Compatibility: Exposing Mongo.Collection instances aligns with Meteor's conventions, supporting reactivity and publication/subscription patterns.
  • No Breaking Changes: This is an additive feature, preserving the existing API.
  • Reference Integration: Compatible with accounts-lea (per CONTRIBUTING.md), as it doesn't alter the OAuth2 flow or model behavior.
  • Implementation Details: Requires exporting collections from lib/model/meteor-model.js, assigning it in OAuthMeteorModel, and adding the getter in lib/oauth.js. Tests and JSDoc will be included, and API.md will be updated via meteor npm run build:docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions