Search code examples
http-redirectroutesauthorizationserver-side

should I use 301 or 302 redirect for access control?


I provide users with file/media links that are only references to the actual file. One the server side I intercept this link and check if the user has access to said file. If they do I redirect them to the final link that contains a expiriable policy for what they can do to the resource and for how long.

My question is if a 302 redirect makes more sense than a 301 and/or if there is a better way to handle this?

if user
    file = share.Files.findOne({_id: @params._id, accessibleBy: user._id})
    if file
      location = "/server/file/" + file._id + "/" + file.policy + "/" + file.signature
      @response.writeHead(301, {Location: location})
  @response.end()

Solution

  • A 301 means ‘moved permanently’—if the URL is ever valid, no, it hasn’t moved permanently. A 302 (‘Found’) would be better, yes, but 303 (‘See Other’) would be even more appropriate.