betagouv/service-national-universel

View on GitHub
api/src/auth.js

Summary

Maintainability
F
3 days
Test Coverage

File auth.js has 842 lines of code (exceeds 700 allowed). Consider refactoring.
Open

const jwt = require("jsonwebtoken");
const crypto = require("crypto");
const Joi = require("joi");
const { getDb } = require("./mongo");

Severity: Major
Found in api/src/auth.js - About 5 hrs to fix

    Function signin has a Cognitive Complexity of 35 (exceeds 5 allowed). Consider refactoring.
    Open

      async signin(req, res) {
        const { error, value } = Joi.object({ email: Joi.string().lowercase().trim().email().required(), password: Joi.string().required() }).unknown().validate(req.body);
        if (error) return res.status(400).send({ ok: false, code: ERRORS.EMAIL_AND_PASSWORD_REQUIRED });
    
        const { password, email } = value;
    Severity: Minor
    Found in api/src/auth.js - About 5 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function signupCLE has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
    Open

      async signupCLE(req, res) {
        try {
          let schema = {
            email: Joi.string().lowercase().trim().email().required(),
            phone: Joi.string().trim().required(),
    Severity: Minor
    Found in api/src/auth.js - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function signupVolontaire has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
    Open

      async signupVolontaire(req, res) {
        try {
          const { error, value } = Joi.object({
            email: Joi.string().lowercase().trim().email().required(),
            phone: Joi.string().trim().required(),
    Severity: Minor
    Found in api/src/auth.js - About 2 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function signin2FA has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
    Open

      async signin2FA(req, res) {
        try {
          const { error, value } = Joi.object({
            email: Joi.string().lowercase().trim().email().required(),
            token_2fa: Joi.string().required(),
    Severity: Minor
    Found in api/src/auth.js - About 1 hr to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function resetPassword has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

      async resetPassword(req, res) {
        const { error, value } = Joi.object({
          password: Joi.string().required(),
          newPassword: Joi.string().required(),
          verifyPassword: Joi.string().required(),
    Severity: Minor
    Found in api/src/auth.js - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function checkPassword has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
    Open

      async checkPassword(req, res) {
        const { error, value } = Joi.object({
          password: Joi.string().required(),
        })
          .unknown()
    Severity: Minor
    Found in api/src/auth.js - About 55 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function validateEmail has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
    Open

      async validateEmail(req, res) {
        try {
          const { error, value } = Joi.object({ token_email_validation: Joi.string().required() }).unknown().validate(req.body);
          if (error) return res.status(400).send({ ok: false, code: ERRORS.INVALID_BODY });
          const { token_email_validation } = value;
    Severity: Minor
    Found in api/src/auth.js - About 45 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function validateEmailUpdate has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
    Open

      async validateEmailUpdate(req, res) {
        try {
          const { error, value } = Joi.object({ token_email_validation: Joi.string().required() }).unknown().validate(req.body);
          if (error) return res.status(400).send({ ok: false, code: ERRORS.INVALID_BODY });
          const { token_email_validation } = value;
    Severity: Minor
    Found in api/src/auth.js - About 35 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function forgotPasswordReset has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
    Open

      async forgotPasswordReset(req, res) {
        const { error, value } = Joi.object({ password: Joi.string().required(), token: Joi.string().min(16).required() })
          .unknown()
          .validate(req.body);
    
    
    Severity: Minor
    Found in api/src/auth.js - About 35 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function requestEmailUpdate has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
    Open

      async requestEmailUpdate(req, res) {
        try {
          const { error, value } = Joi.object({ email: Joi.string().lowercase().trim().email().required(), password: Joi.string().required() }).unknown().validate(req.body);
          if (error) {
            capture(error);
    Severity: Minor
    Found in api/src/auth.js - About 25 mins to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Identical blocks of code found in 2 locations. Consider refactoring.
    Open

          if (isEmailValidationEnabled) {
            await sendTemplate(SENDINBLUE_TEMPLATES.SIGNUP_EMAIL_VALIDATION, {
              emailTo: [{ name: `${user.firstName} ${user.lastName}`, email }],
              params: {
                registration_code: tokenEmailValidation,
    Severity: Major
    Found in api/src/auth.js and 1 other location - About 3 hrs to fix
    api/src/auth.js on lines 333..350

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 202.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Identical blocks of code found in 2 locations. Consider refactoring.
    Open

          if (isEmailValidationEnabled) {
            await sendTemplate(SENDINBLUE_TEMPLATES.SIGNUP_EMAIL_VALIDATION, {
              emailTo: [{ name: `${user.firstName} ${user.lastName}`, email }],
              params: {
                registration_code: tokenEmailValidation,
    Severity: Major
    Found in api/src/auth.js and 1 other location - About 3 hrs to fix
    api/src/auth.js on lines 183..200

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 202.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Identical blocks of code found in 2 locations. Consider refactoring.
    Open

          if (error) {
            if (error.details[0].path.find((e) => e === "email")) return res.status(400).send({ ok: false, user: null, code: ERRORS.EMAIL_INVALID });
            if (error.details[0].path.find((e) => e === "password")) return res.status(400).send({ ok: false, user: null, code: ERRORS.PASSWORD_NOT_VALIDATED });
            return res.status(400).send({ ok: false, code: ERRORS.INVALID_PARAMS });
          }
    Severity: Major
    Found in api/src/auth.js and 1 other location - About 2 hrs to fix
    api/src/auth.js on lines 243..247

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 183.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    Identical blocks of code found in 2 locations. Consider refactoring.
    Open

          if (error) {
            if (error.details[0].path.find((e) => e === "email")) return res.status(400).send({ ok: false, user: null, code: ERRORS.EMAIL_INVALID });
            if (error.details[0].path.find((e) => e === "password")) return res.status(400).send({ ok: false, user: null, code: ERRORS.PASSWORD_NOT_VALIDATED });
            return res.status(400).send({ ok: false, code: ERRORS.INVALID_PARAMS });
          }
    Severity: Major
    Found in api/src/auth.js and 1 other location - About 2 hrs to fix
    api/src/auth.js on lines 102..106

    Duplicated Code

    Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

    Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

    When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

    Tuning

    This issue has a mass of 183.

    We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

    The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

    If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

    See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

    Refactorings

    Further Reading

    There are no issues that match your filters.

    Category
    Status