Skip to content

Instantly share code, notes, and snippets.

@JulienBreux
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JulienBreux/ac0fb6a5f86869e046b0 to your computer and use it in GitHub Desktop.
Save JulienBreux/ac0fb6a5f86869e046b0 to your computer and use it in GitHub Desktop.
Micro-Server to validate oAuth2 roles :)
# How to use:
#
# $ruby server.rb
# $curl --header "X-Company-Roles: manager" --header "X-Company-Roles: admin" http://localhost:2345/
require 'socket'
require 'json'
server = TCPServer.new('localhost', 2345)
loop do
socket = server.accept
request = socket.gets
#STDERR.puts request
response = {:roles => []}
roles = []
while line = socket.gets
matches = /^X-([a-zA-Z]+)-Roles\: ([a-z]+)$/.match(line.chomp)
if matches
response[:roles] << matches.captures[1]
end
break if line =~ /^\s*$/
end
socket.print "HTTP/1.1 200 OK\r\n" +
"Content-Type: application/json\r\n" +
"Content-Length: #{response.to_json.bytesize}\r\n" +
"Connection: close\r\n"
socket.print "\r\n"
socket.print response.to_json
socket.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment