Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some bugs, improved documentation and changed constants' names #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions gingae.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Gin middleware, handling authentication against the Google App Engine users
// service.
// Package gingae provides Gin middleware, handling authentication against the
// Google App Engine users service.
package gingae

import (
Expand All @@ -9,14 +9,19 @@ import (
)

const (
Context = "GaeContext"
User = "GaeUser"
UserOAuthError = "GaeUserOAuthError"
// ContextKey is the key name for the GAE context within the Gin context
ContextKey = "GaeContext"

// UserKey is the key name for the GAE user within the Gin context
UserKey = "GaeUser"

// UserOAuthErrorKey is the key name for an OAuth error message within the Gin context
UserOAuthErrorKey = "GaeUserOAuthError"
)

type gaeContextProvider func(c *gin.Context) appengine.Context

// Set a variable on the Gin context, containing the GAE Context.
// GaeContext sets a variable on the Gin context, containing the GAE Context.
func GaeContext() gin.HandlerFunc {
return gaeContextFromProvider(func(c *gin.Context) appengine.Context {
return appengine.NewContext(c.Request)
Expand All @@ -26,34 +31,34 @@ func GaeContext() gin.HandlerFunc {
func gaeContextFromProvider(gaeContextProvider gaeContextProvider) gin.HandlerFunc {
return func(c *gin.Context) {
gaeCtx := gaeContextProvider(c)
c.Set(Context, gaeCtx)
c.Set(ContextKey, gaeCtx)
}
}

// Set a variable on the Gin context, containing the GAE User.
// GaeUser sets a variable on the Gin context, containing the GAE User.
func GaeUser() gin.HandlerFunc {
return func(c *gin.Context) {
gaeCtx, err := c.Get(Context)
if err != nil {
gaeCtx, exists := c.Get(ContextKey)
if exists == false {
panic("Must use the GaeContext middleware before the GaeUser")
}
gaeUser := user.Current(gaeCtx.(appengine.Context))
c.Set(User, gaeUser)
c.Set(UserKey, gaeUser)
}
}

// Set a variable on the Gin context, containing the GAE User, logged in using OAuth.
// GaeUserOAuth sets a variable on the Gin context, containing the GAE User, logged in using OAuth.
func GaeUserOAuth(scope string) gin.HandlerFunc {
return func(c *gin.Context) {
gaeCtx, err := c.Get(Context)
if err != nil {
gaeCtx, exists := c.Get(ContextKey)
if exists == false {
panic("Must use the GaeContext middleware before the GaeUserOAuth")
}
gaeUser, err := user.CurrentOAuth(gaeCtx.(appengine.Context), scope)
if err != nil {
c.Set(UserOAuthError, err)
c.Set(UserOAuthErrorKey, err)
} else {
c.Set(User, gaeUser)
c.Set(UserKey, gaeUser)
}
}
}