-
Notifications
You must be signed in to change notification settings - Fork 12
Description
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
- Using
Mongo.getCollection
from the MongoDB driver: This would expose raw MongoDB collections, bypassing Meteor'sMongo.Collection
abstraction. It risks breaking Meteor's reactivity, security rules, and OAuth2 compliance, and is less user-friendly for Meteor developers. - Adding specific query methods to
OAuthMeteorModel
: Instead of exposing collections, we could add methods likefindAccessTokens
orcreateIndex
. However, this would require anticipating all possible use cases, leading to a bloated API and maintenance overhead. - 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 useOAuthMeteorModel
methods likesaveToken
orrevokeAuthorizationCode
. - 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
(perCONTRIBUTING.md
), as it doesn't alter the OAuth2 flow or model behavior. - Implementation Details: Requires exporting
collections
fromlib/model/meteor-model.js
, assigning it inOAuthMeteorModel
, and adding the getter inlib/oauth.js
. Tests and JSDoc will be included, andAPI.md
will be updated viameteor npm run build:docs
.