<commit_before>var db = require('../models/db'),
    model = {};

// Functions always required by node-oauth2-server
model.getAccessToken = function(accessToken, callback) {
  db.OauthAccessToken
    .find({ where: { accessToken: accessToken } })
    .success(function(token){
      if (token) {
        callback(null, token);
      }else{
        callback(null, false)
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.getClient = function(clientId, clientSecret, callback) {
  db.OauthClient
    .find({ where: { clientId: clientId } })
    .success(function(client){
      if (client && clientSecret && (clientSecret !== client.clientSecret) ){
        callback(null, false);
      }else{
        callback(null, client);
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.grantTypeAllowed = function(clientId, grantType, callback) {
  db.OauthClient
    .find({ where: { clientId: clientId } })
    .success(function(client){
      var regex = new RegExp(grantType, 'i'),
          allowed = false;

      if ((client) && (client.grantTypeAllowed.search(regex) > -1)){
        allowed = true
      }else{
        allowed = false
      }

      callback(null, allowed);
    })
    .error(function(err){
      callback(err, false);
    });
};

model.saveAccessToken = function(accessToken, clientId, expires, user, callback) {
  var newToken = db.OauthAccessToken.build({
    accessToken: accessToken,
    clientId: clientId,
    userId: user.id,
    expires: expires
  });
  newToken
    .save()
    .success(function(persistedToken){
      callback(false);
    })
    .error(function(err){
      callback(err);
    })
};

// Required by GrantType authorization_code
model.getAuthCode = function(authCode, callback) {
  db.OauthAuthCode
    .find({ where: { authCode: authCode } })
    .success(function(code){
      if (code){
        callback(null, code);
      }else{
        callback(null, false);
      }
    })
    .error(function(err){
        callback(err, false);
    });
};

model.saveAuthCode = function(authCode, clientId, expires, userId, callback){
  var newCode = db.OauthAuthCode.build({
    authCode: authCode,
    clientId: clientId,
    userId: userId,
    expires: expires,
  });

  newCode
    .save()
    .success(function(persistedCode){
      callback(false);
    })
    .error(function(err){
      callback(err);
    });
};

// Required by GrantType refresh_token
model.getRefreshToken = function(refreshToken, callback) {
  db.OauthRefreshToken
    .find({ where: { refreshToken: refreshToken } })
    .success(function(token){
      if (token){
        callback(null, refreshToken);
      }else{
        callback(null, false);
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.saveRefreshToken = function(refreshToken, clientId, expires, user, callback) {
  var newToken = db.OauthRefreshToken.build({
    refreshToken: refreshToken,
    clientId: clientId,
    userId: user.id,
    expires: expires,
  });<commit_msg>Fix bug in oauth model, refresh token not working due to incorrect var.<commit_after>var db = require('../models/db'),
    model = {};

// Functions always required by node-oauth2-server
model.getAccessToken = function(accessToken, callback) {
  db.OauthAccessToken
    .find({ where: { accessToken: accessToken } })
    .success(function(token){
      if (token) {
        callback(null, token);
      }else{
        callback(null, false)
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.getClient = function(clientId, clientSecret, callback) {
  db.OauthClient
    .find({ where: { clientId: clientId } })
    .success(function(client){
      if (client && clientSecret && (clientSecret !== client.clientSecret) ){
        callback(null, false);
      }else{
        callback(null, client);
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.grantTypeAllowed = function(clientId, grantType, callback) {
  db.OauthClient
    .find({ where: { clientId: clientId } })
    .success(function(client){
      var regex = new RegExp(grantType, 'i'),
          allowed = false;

      if ((client) && (client.grantTypeAllowed.search(regex) > -1)){
        allowed = true
      }else{
        allowed = false
      }

      callback(null, allowed);
    })
    .error(function(err){
      callback(err, false);
    });
};

model.saveAccessToken = function(accessToken, clientId, expires, user, callback) {
  var newToken = db.OauthAccessToken.build({
    accessToken: accessToken,
    clientId: clientId,
    userId: user.id,
    expires: expires
  });
  newToken
    .save()
    .success(function(persistedToken){
      callback(false);
    })
    .error(function(err){
      callback(err);
    })
};

// Required by GrantType authorization_code
model.getAuthCode = function(authCode, callback) {
  db.OauthAuthCode
    .find({ where: { authCode: authCode } })
    .success(function(code){
      if (code){
        callback(null, code);
      }else{
        callback(null, false);
      }
    })
    .error(function(err){
        callback(err, false);
    });
};

model.saveAuthCode = function(authCode, clientId, expires, userId, callback){
  var newCode = db.OauthAuthCode.build({
    authCode: authCode,
    clientId: clientId,
    userId: userId,
    expires: expires,
  });

  newCode
    .save()
    .success(function(persistedCode){
      callback(false);
    })
    .error(function(err){
      callback(err);
    });
};

// Required by GrantType refresh_token
model.getRefreshToken = function(refreshToken, callback) {
  db.OauthRefreshToken
    .find({ where: { refreshToken: refreshToken } })
    .success(function(token){
      if (token){
        callback(null, token);
      }else{
        callback(null, false);
      }
    })
    .error(function(err){
      callback(err, false);
    });
};

model.saveRefreshToken = function(refreshToken, clientId, expires, user, callback) {
  var newToken = db.OauthRefreshToken.build({
    refreshToken: refreshToken,
    clientId: clientId,
    userId: user.id,
    expires: expires,
  });
{'fraction_non_alphanumeric': 0.14209245742092458, 'fraction_numerical': 0.0006488240064882401, 'mean_word_length': 2.801479654747226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1596857', 'n_tokens_mistral': 1914, 'n_tokens_neox': 1899, 'n_words': 394}