Proper behaviour for publicly available api resource requested using expired authorization token












0














I know there are already many 401 vs 403 questions, but that's not my case.



What i'm asking is: what should a server ideally do if the requested api resource is publicly available (does not require authorization) but the request includes an authorization header with a token that is either incorrect or expired?



My reasoning is as follows: in theory server could ignore it and respond, but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not. Client app should also get an instant feedback that it should reauthenticate, and not only if and when it hits a non-public end-point.



Being "badly authenticated" and therefore implicitly treated as an anonymous is a somewhat undefined and confusing behaviour i think.



So to sum up. Is it justified to use 400 in this particular case or is there any other, common practise?










share|improve this question



























    0














    I know there are already many 401 vs 403 questions, but that's not my case.



    What i'm asking is: what should a server ideally do if the requested api resource is publicly available (does not require authorization) but the request includes an authorization header with a token that is either incorrect or expired?



    My reasoning is as follows: in theory server could ignore it and respond, but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not. Client app should also get an instant feedback that it should reauthenticate, and not only if and when it hits a non-public end-point.



    Being "badly authenticated" and therefore implicitly treated as an anonymous is a somewhat undefined and confusing behaviour i think.



    So to sum up. Is it justified to use 400 in this particular case or is there any other, common practise?










    share|improve this question

























      0












      0








      0







      I know there are already many 401 vs 403 questions, but that's not my case.



      What i'm asking is: what should a server ideally do if the requested api resource is publicly available (does not require authorization) but the request includes an authorization header with a token that is either incorrect or expired?



      My reasoning is as follows: in theory server could ignore it and respond, but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not. Client app should also get an instant feedback that it should reauthenticate, and not only if and when it hits a non-public end-point.



      Being "badly authenticated" and therefore implicitly treated as an anonymous is a somewhat undefined and confusing behaviour i think.



      So to sum up. Is it justified to use 400 in this particular case or is there any other, common practise?










      share|improve this question













      I know there are already many 401 vs 403 questions, but that's not my case.



      What i'm asking is: what should a server ideally do if the requested api resource is publicly available (does not require authorization) but the request includes an authorization header with a token that is either incorrect or expired?



      My reasoning is as follows: in theory server could ignore it and respond, but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not. Client app should also get an instant feedback that it should reauthenticate, and not only if and when it hits a non-public end-point.



      Being "badly authenticated" and therefore implicitly treated as an anonymous is a somewhat undefined and confusing behaviour i think.



      So to sum up. Is it justified to use 400 in this particular case or is there any other, common practise?







      rest api http-status-codes






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 at 8:53









      plunntic iam

      591615




      591615
























          1 Answer
          1






          active

          oldest

          votes


















          0














          If this API itself is publicly available, and by publicly available you mean doesn't require any authentication to the user, why will you return to the user a response with Bad Request? Think to yourself if it makes sense, if its public, you can bypass any kind of validation and save some processing on the server.



          Related with this:



          "but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not."



          I think that in this case, you should change the API itself to require authentication, and not accept any kind of unauthorized user, or a user with an expired token.



          Sorry if I didn't help much, but this is my opinion on this.
          If its badly authenticated then it's not a public API, right?
          A public API would respond in any scenario, could just have some restriction on requests by second.






          share|improve this answer





















          • I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
            – plunntic iam
            Nov 24 at 5:46










          • Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
            – plunntic iam
            Nov 24 at 5:53











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443355%2fproper-behaviour-for-publicly-available-api-resource-requested-using-expired-aut%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          If this API itself is publicly available, and by publicly available you mean doesn't require any authentication to the user, why will you return to the user a response with Bad Request? Think to yourself if it makes sense, if its public, you can bypass any kind of validation and save some processing on the server.



          Related with this:



          "but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not."



          I think that in this case, you should change the API itself to require authentication, and not accept any kind of unauthorized user, or a user with an expired token.



          Sorry if I didn't help much, but this is my opinion on this.
          If its badly authenticated then it's not a public API, right?
          A public API would respond in any scenario, could just have some restriction on requests by second.






          share|improve this answer





















          • I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
            – plunntic iam
            Nov 24 at 5:46










          • Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
            – plunntic iam
            Nov 24 at 5:53
















          0














          If this API itself is publicly available, and by publicly available you mean doesn't require any authentication to the user, why will you return to the user a response with Bad Request? Think to yourself if it makes sense, if its public, you can bypass any kind of validation and save some processing on the server.



          Related with this:



          "but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not."



          I think that in this case, you should change the API itself to require authentication, and not accept any kind of unauthorized user, or a user with an expired token.



          Sorry if I didn't help much, but this is my opinion on this.
          If its badly authenticated then it's not a public API, right?
          A public API would respond in any scenario, could just have some restriction on requests by second.






          share|improve this answer





















          • I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
            – plunntic iam
            Nov 24 at 5:46










          • Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
            – plunntic iam
            Nov 24 at 5:53














          0












          0








          0






          If this API itself is publicly available, and by publicly available you mean doesn't require any authentication to the user, why will you return to the user a response with Bad Request? Think to yourself if it makes sense, if its public, you can bypass any kind of validation and save some processing on the server.



          Related with this:



          "but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not."



          I think that in this case, you should change the API itself to require authentication, and not accept any kind of unauthorized user, or a user with an expired token.



          Sorry if I didn't help much, but this is my opinion on this.
          If its badly authenticated then it's not a public API, right?
          A public API would respond in any scenario, could just have some restriction on requests by second.






          share|improve this answer












          If this API itself is publicly available, and by publicly available you mean doesn't require any authentication to the user, why will you return to the user a response with Bad Request? Think to yourself if it makes sense, if its public, you can bypass any kind of validation and save some processing on the server.



          Related with this:



          "but this feels like a very bad idea. For example business logic behind processing the request could differ depending on being authenticated or not."



          I think that in this case, you should change the API itself to require authentication, and not accept any kind of unauthorized user, or a user with an expired token.



          Sorry if I didn't help much, but this is my opinion on this.
          If its badly authenticated then it's not a public API, right?
          A public API would respond in any scenario, could just have some restriction on requests by second.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 9:01









          TiagoM

          1,48542560




          1,48542560












          • I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
            – plunntic iam
            Nov 24 at 5:46










          • Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
            – plunntic iam
            Nov 24 at 5:53


















          • I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
            – plunntic iam
            Nov 24 at 5:46










          • Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
            – plunntic iam
            Nov 24 at 5:53
















          I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
          – plunntic iam
          Nov 24 at 5:46




          I'd have to disagree. Let's say i have a CMS where both the front- and back-end are SPAs. Unless you have some specific requirement - which i don't have - it makes perfect sense to create one api serving both applications. And there you have it - some end-points require authorization, some don't, and some differ in behaviour depending on authorization. For example if you're authenticated as an admin you're getting some non-public data about posts, users which anonymous users shouldn't ever see.
          – plunntic iam
          Nov 24 at 5:46












          Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
          – plunntic iam
          Nov 24 at 5:53




          Anyway, my point was, that a request with invalid authorization is indeed bad / broken. Client assumes - for example - his authenticated as an admin, but api can't respond the way client assumes because authorization is invalid at this point. But responding with only the public data may be (and probably is) something totaly different than what the client expects. That's why i think api should respond with 400 (or with other bad request type of code).
          – plunntic iam
          Nov 24 at 5:53


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443355%2fproper-behaviour-for-publicly-available-api-resource-requested-using-expired-aut%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks