<commit_before>/**
 * OAuth 2.0 request validation.
 *
 * This component provides a function that validates a client's authorization
 * request.  If the request is valid, the function yeilds a `client` object and
 * a `redirectURI`.  The client object provides metadata about the client, such
 * as name and logo, used during processing of the authorization request.  The
 * redirect URI is used when responding to the authorization request via HTTP
 * redirections.
 *
 * If the request is invalid, the function yeilds an error.  It is expected that
 * the application will inform the user of the error, and must not automatically
 * redirect the user to an invalid redirection URI.
 */
exports = module.exports = function(realms) {
  var oauth2orize = require('oauth2orize')
    , uri = require('url');
  
  return function validateClient(clientID, redirectURI, cb) {
    
    realms.get(clientID, 'clients', function(err, client) {
      console.log('GOT CLIENT!');
      console.log(err);
      console.log(client);
      //return;
    
      //var dir = realm.createDirectory(function() {
        // The directory is ready, continue processing by fetching the client
        // record.
        
        //dir.get(clientID, function(err, client) {
          if (err) { return cb(err); }
          if (!client) {
            return cb(new oauth2orize.AuthorizationError('Unknown client', 'unauthorized_client'));
          }
          if (!client.redirectURIs || !client.redirectURIs.length) {
            // The client has not registered any redirection endpoints.  Such
            // clients are not authorized to use the authorization endpoint.
            //
            // This enforcement is in place in order to prevent the authorization
            // server from functioning as an open redirector.  Refer to Section
            // 3.1.2.2 of RFC 6749 for further details.
            return cb(new oauth2orize.AuthorizationError('Client has no registered redirect URIs', 'unauthorized_client'));
          }
          if (client.redirectURIs.length > 1 && !redirectURI) {
            // If multiple redirection URIs have been registered, the client must
            // include a redirection URI with the authorization request.  Refer to
            // Section 3.1.2.3 of RFC 6749 for further details.
            return cb(new oauth2orize.AuthorizationError('Missing required parameter: redirect_uri', 'invalid_request'));
          }
      
      
          if (redirectURI) {
            var url = uri.parse(redirectURI);
            //console.log(url);
            if (url.protocol == 'storagerelay:') {
              // TODO: Implement web/js origin checks
              return cb(null, client, redirectURI, 'http://localhost:3001');
            }
          }
      
      
          if (redirectURI && client.redirectURIs.indexOf(redirectURI) == -1) {
            return cb(new oauth2orize.AuthorizationError('Client not permitted to use redirect URI', 'unauthorized_client'));
          }
      
          return cb(null, client, redirectURI || client.redirectURIs[0], 'http://localhost:3001');
        //});
      //}); // realm.createDirectory(readyListener)
      
      // TODO: Handle dir.on('error')??
      
    }); // realms.resolve
  };
};

exports['@require'] = [ 'http://schemas.modulate.io/js/aaa/realms' ];<commit_msg>Remove dead code and fix indentation.<commit_after>/**
 * OAuth 2.0 request validation.
 *
 * This component provides a function that validates a client's authorization
 * request.  If the request is valid, the function yeilds a `client` object and
 * a `redirectURI`.  The client object provides metadata about the client, such
 * as name and logo, used during processing of the authorization request.  The
 * redirect URI is used when responding to the authorization request via HTTP
 * redirections.
 *
 * If the request is invalid, the function yeilds an error.  It is expected that
 * the application will inform the user of the error, and must not automatically
 * redirect the user to an invalid redirection URI.
 */
exports = module.exports = function(realms) {
  var oauth2orize = require('oauth2orize')
    , uri = require('url');
  
  return function validateClient(clientID, redirectURI, cb) {
    
    realms.get(clientID, 'clients', function(err, client) {
      if (err) { return cb(err); }
      if (!client) {
        return cb(new oauth2orize.AuthorizationError('Unknown client', 'unauthorized_client'));
      }
      if (!client.redirectURIs || !client.redirectURIs.length) {
        // The client has not registered any redirection endpoints.  Such
        // clients are not authorized to use the authorization endpoint.
        //
        // This enforcement is in place in order to prevent the authorization
        // server from functioning as an open redirector.  Refer to Section
        // 3.1.2.2 of RFC 6749 for further details.
        return cb(new oauth2orize.AuthorizationError('Client has no registered redirect URIs', 'unauthorized_client'));
      }
      if (client.redirectURIs.length > 1 && !redirectURI) {
        // If multiple redirection URIs have been registered, the client must
        // include a redirection URI with the authorization request.  Refer to
        // Section 3.1.2.3 of RFC 6749 for further details.
        return cb(new oauth2orize.AuthorizationError('Missing required parameter: redirect_uri', 'invalid_request'));
      }
  
  
      if (redirectURI) {
        var url = uri.parse(redirectURI);
        //console.log(url);
        if (url.protocol == 'storagerelay:') {
          // TODO: Implement web/js origin checks
          return cb(null, client, redirectURI, 'http://localhost:3001');
        }
      }
  
  
      if (redirectURI && client.redirectURIs.indexOf(redirectURI) == -1) {
        return cb(new oauth2orize.AuthorizationError('Client not permitted to use redirect URI', 'unauthorized_client'));
      }
  
      return cb(null, client, redirectURI || client.redirectURIs[0], 'http://localhost:3001');
    }); // realms.resolve
  };
};

exports['@require'] = [ 'http://schemas.modulate.io/js/aaa/realms' ];
{'fraction_non_alphanumeric': 0.10206739378153996, 'fraction_numerical': 0.011395083835259645, 'mean_word_length': 2.618374558303887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2641426', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1616, 'n_words': 563}