Get the authentification code and translate it to an access token.

Introduced properties

fun build_auth_body(code: String): HeaderMap

popcorn :: GithubOAuthCallBack :: build_auth_body

Build the OAuth post data.
fun client_id: String

popcorn :: GithubOAuthCallBack :: client_id

The client ID delivered by GitHub for your application.
fun client_id=(client_id: String)

popcorn :: GithubOAuthCallBack :: client_id=

The client ID delivered by GitHub for your application.
fun client_secret: String

popcorn :: GithubOAuthCallBack :: client_secret

The client secret you received from Github when your registered your application.
fun client_secret=(client_secret: String)

popcorn :: GithubOAuthCallBack :: client_secret=

The client secret you received from Github when your registered your application.
init defaultinit(client_id: String, client_secret: String, redirect_uri: nullable String, state: nullable String)

popcorn :: GithubOAuthCallBack :: defaultinit

fun headers: HeaderMap

popcorn :: GithubOAuthCallBack :: headers

Header map sent with the OAuth token request.
protected fun headers=(headers: HeaderMap)

popcorn :: GithubOAuthCallBack :: headers=

Header map sent with the OAuth token request.
fun redirect_uri: nullable String

popcorn :: GithubOAuthCallBack :: redirect_uri

The URL in your application where users will be sent after authorization.
fun redirect_uri=(redirect_uri: nullable String)

popcorn :: GithubOAuthCallBack :: redirect_uri=

The URL in your application where users will be sent after authorization.
fun state: nullable String

popcorn :: GithubOAuthCallBack :: state

An optional and unguessable random string.
fun state=(state: nullable String)

popcorn :: GithubOAuthCallBack :: state=

An optional and unguessable random string.
fun token_url: String

popcorn :: GithubOAuthCallBack :: token_url

Github OAuth token URL.
fun token_url=(token_url: String)

popcorn :: GithubOAuthCallBack :: token_url=

Github OAuth token URL.

Redefined properties

redef type SELF: GithubOAuthCallBack

popcorn $ GithubOAuthCallBack :: SELF

Type of this instance, automatically specialized in every class
redef fun get(req: HttpRequest, res: HttpResponse)

popcorn $ GithubOAuthCallBack :: get

GET handler.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type BODY: Serializable

popcorn :: Handler :: BODY

Kind of objects returned by deserialize_body
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
fun all(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: all

Handler to all kind of HTTP request methods.
fun build_auth_body(code: String): HeaderMap

popcorn :: GithubOAuthCallBack :: build_auth_body

Build the OAuth post data.
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun client_id: String

popcorn :: GithubOAuthCallBack :: client_id

The client ID delivered by GitHub for your application.
fun client_id=(client_id: String)

popcorn :: GithubOAuthCallBack :: client_id=

The client ID delivered by GitHub for your application.
fun client_secret: String

popcorn :: GithubOAuthCallBack :: client_secret

The client secret you received from Github when your registered your application.
fun client_secret=(client_secret: String)

popcorn :: GithubOAuthCallBack :: client_secret=

The client secret you received from Github when your registered your application.
init defaultinit(client_id: String, client_secret: String, redirect_uri: nullable String, state: nullable String)

popcorn :: GithubOAuthCallBack :: defaultinit

fun delete(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: delete

DELETE handler.
fun deserialize_body(req: HttpRequest, res: HttpResponse): nullable BODY

popcorn :: Handler :: deserialize_body

Deserialize the request body
fun get(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: get

GET handler.
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
fun headers: HeaderMap

popcorn :: GithubOAuthCallBack :: headers

Header map sent with the OAuth token request.
protected fun headers=(headers: HeaderMap)

popcorn :: GithubOAuthCallBack :: headers=

Header map sent with the OAuth token request.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun post(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: post

POST handler.
fun put(req: HttpRequest, res: HttpResponse)

popcorn :: Handler :: put

PUT handler.
fun redirect_uri: nullable String

popcorn :: GithubOAuthCallBack :: redirect_uri

The URL in your application where users will be sent after authorization.
fun redirect_uri=(redirect_uri: nullable String)

popcorn :: GithubOAuthCallBack :: redirect_uri=

The URL in your application where users will be sent after authorization.
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun state: nullable String

popcorn :: GithubOAuthCallBack :: state

An optional and unguessable random string.
fun state=(state: nullable String)

popcorn :: GithubOAuthCallBack :: state=

An optional and unguessable random string.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun token_url: String

popcorn :: GithubOAuthCallBack :: token_url

Github OAuth token URL.
fun token_url=(token_url: String)

popcorn :: GithubOAuthCallBack :: token_url=

Github OAuth token URL.
fun validate_body(req: HttpRequest, res: HttpResponse): nullable String

popcorn :: Handler :: validate_body

Validate body input with validator
fun validator: nullable DocumentValidator

popcorn :: Handler :: validator

Validator used to check body input
protected fun validator=(validator: nullable DocumentValidator)

popcorn :: Handler :: validator=

Validator used to check body input
package_diagram popcorn::GithubOAuthCallBack GithubOAuthCallBack popcorn::Handler Handler popcorn::GithubOAuthCallBack->popcorn::Handler core::Object Object popcorn::Handler->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class Handler

popcorn :: Handler

Class handler for a route.

Class definitions

popcorn $ GithubOAuthCallBack
# Get the authentification code and translate it to an access token.
class GithubOAuthCallBack
	super Handler

	# The client ID delivered by GitHub for your application.
	#
	# See https://github.com/settings/applications/new.
	var client_id: String is writable

	# The client secret you received from Github when your registered your application.
	var client_secret: String is writable

	# The URL in your application where users will be sent after authorization.
	#
	# If `null`, the URL used in application registration will be used.
	#
	# See https://developer.github.com/v3/oauth/#redirect-urls.
	var redirect_uri: nullable String is writable

	# An optional and unguessable random string.
	#
	# It is used to protect against cross-site request forgery attacks.
	var state: nullable String is writable

	# Github OAuth token URL.
	var token_url = "https://github.com/login/oauth/access_token" is writable

	# Header map sent with the OAuth token request.
	var headers: HeaderMap do
		var map = new HeaderMap
		map["Accept"] = "application/json"
		return map
	end

	# Build the OAuth post data.
	fun build_auth_body(code: String): HeaderMap do
		var map = new HeaderMap
		map["client_id"] = client_id
		map["client_secret"] = client_secret
		map["code"] = code
		var redirect_uri = self.redirect_uri
		if redirect_uri != null then map["redirect_uri"] = redirect_uri
		var state = self.state
		if state != null then map["state"] = state
		return map
	end

	redef fun get(req, res) do
		# Get OAuth code
		var code = req.string_arg("code")
		if code == null then
			res.error 401
			return
		end

		# Exchange it for an access token
		var access_token = request_access_token(code)
		if access_token == null then
			res.error 401
			return
		end

		# Load github user
		var gh_api = new GithubAPI(access_token)
		var user = gh_api.get_auth_user
		if user == null then
			res.error 401
			return
		end
		# Set session and redirect to user page
		var session = req.session
		if session == null then
			res.error 500
			return
		end
		session.user = user
		res.redirect redirect_uri or else "/"
	end

	# Request an access token from an access `code`.
	private fun request_access_token(code: String): nullable String do
		var request = new CurlHTTPRequest(token_url)
		request.headers = headers
		request.data = build_auth_body(code)
		var response = request.execute
		return parse_token_response(response)
	end

	# Parse the Github access_token response and extract the access_token.
	private fun parse_token_response(response: CurlResponse): nullable String do
		if response isa CurlResponseFailed then
			print "Request to Github OAuth failed"
			print "Requested URI: {token_url}"
			print "Error code: {response.error_code}"
			print "Error msg: {response.error_msg}"
			return null
		else if response isa CurlResponseSuccess then
			var obj = response.body_str.parse_json
			if not obj isa JsonObject then
				print "Error: Cannot parse json response"
				print response.body_str
				return null
			end
			var access_token = obj.get_or_null("access_token")
			if not access_token isa String then
				print "Error: No `access_token` key in response"
				print obj.to_json
				return null
			end
			return access_token
		end
		return null
	end
end
lib/popcorn/pop_auth.nit:138,1--251,3