From 56f78567a212cabcdc7d871b4340d151d6e076c5 Mon Sep 17 00:00:00 2001 From: Makis Maropoulos Date: Wed, 22 Jun 2016 12:45:57 +0300 Subject: [PATCH] New: OAuth2 support via gothic. Example: https://github.com/iris-contrib/gothic/blob/master/example/main.go https://github.com/iris-contrib/gothic/blob/master/example/main.go --- README.md | 2 +- THIRDPARTY.md | 1 + context.go | 25 +++++++++++++------------ context/context.go | 16 ++++++---------- iris.go | 6 +++--- 5 files changed, 24 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 58635e76..ae3266c4 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ func main() { func myAuthMiddleware(c *iris.Context) { s := c.Session() - if s.GetString("username") == "myusername" && s.GetString("passowrd") == "mypassword" { + if s.GetString("username") == "myusername" && s.GetString("password") == "mypassword" { c.Next() } else { c.EmitError(iris.StatusUnauthorized) diff --git a/THIRDPARTY.md b/THIRDPARTY.md index 058bcc10..3c386acb 100644 --- a/THIRDPARTY.md +++ b/THIRDPARTY.md @@ -11,3 +11,4 @@ Third party packages - [formam as form binder](https://github.com/monoculum/formam) - [i18n for internalization](https://github.com/Unknwon/i18n) - [color for banner](https://github.com/fatih/color) +- [gothic](https://github.com/iris-contrib/gothic)/[goth for Multi-Provider OAuth, OAuth2](https://github.com/markbates/goth) diff --git a/context.go b/context.go index 27d4cbc1..b5aae14c 100644 --- a/context.go +++ b/context.go @@ -11,16 +11,6 @@ import ( "encoding/json" "encoding/xml" "fmt" - "github.com/iris-contrib/formBinder" - "github.com/iris-contrib/goth" - "github.com/iris-contrib/goth/gothic" - "github.com/kataras/iris/config" - "github.com/kataras/iris/context" - "github.com/kataras/iris/errors" - "github.com/kataras/iris/sessions/store" - "github.com/kataras/iris/utils" - "github.com/klauspost/compress/gzip" - "github.com/valyala/fasthttp" "html/template" "io" "net" @@ -32,6 +22,17 @@ import ( "strings" "sync" "time" + + "github.com/iris-contrib/formBinder" + "github.com/iris-contrib/gothic" + "github.com/kataras/iris/config" + "github.com/kataras/iris/context" + "github.com/kataras/iris/errors" + "github.com/kataras/iris/sessions/store" + "github.com/kataras/iris/utils" + "github.com/klauspost/compress/gzip" + "github.com/markbates/goth" + "github.com/valyala/fasthttp" ) const ( @@ -789,9 +790,9 @@ func (ctx *Context) Log(format string, a ...interface{}) { // process and fetches all of the basic information about the user from the provider. // // It expects to be able to get the name of the provider from the named parameters -// as either "provider" or ":provider". +// as either "provider" or url query parameter ":provider". // -// See https://github.com/iris-contrib/goth/examples/main.go to see this in action. +// See https://github.com/iris-contrib/gothic/blob/master/example/main.go to see this in action. func (ctx *Context) CompleteUserAuth() (goth.User, error) { return gothic.CompleteUserAuth(ctx) } diff --git a/context/context.go b/context/context.go index fca88687..531bdbe3 100644 --- a/context/context.go +++ b/context/context.go @@ -2,12 +2,13 @@ package context import ( "bufio" - "github.com/iris-contrib/goth" - "github.com/kataras/iris/sessions/store" - "github.com/valyala/fasthttp" "html/template" "io" "time" + + "github.com/kataras/iris/sessions/store" + "github.com/markbates/goth" + "github.com/valyala/fasthttp" ) type ( @@ -135,13 +136,8 @@ type ( // IContextAuth handles the authentication/authorization IContextAuth interface { - // CompleteUserAuth does what it says on the tin. It completes the authentication - // process and fetches all of the basic information about the user from the provider. - // - // It expects to be able to get the name of the provider from the named parameters - // as either "provider" or ":provider". - // - // See https://github.com/iris-contrib/goth/examples/main.go to see this in action. + // CompleteUserAuth + // See https://github.com/iris-contrib/gothic/blob/master/example/main.go to see this in action. CompleteUserAuth() (goth.User, error) } ) diff --git a/iris.go b/iris.go index 89180e26..4bed26be 100644 --- a/iris.go +++ b/iris.go @@ -59,7 +59,7 @@ import ( "strings" "time" - "github.com/iris-contrib/goth/gothic" + "github.com/iris-contrib/gothic" "github.com/kataras/iris/config" "github.com/kataras/iris/context" "github.com/kataras/iris/errors" @@ -570,12 +570,12 @@ func (s *Framework) TemplateString(templateFile string, pageContext interface{}, // BeginAuthHandler is a convienence handler for starting the authentication process. // It expects to be able to get the name of the provider from the named parameters -// as either "provider" or ":provider". +// as either "provider" or url query parameter ":provider". // // BeginAuthHandler will redirect the user to the appropriate authentication end-point // for the requested provider. // -// See https://github.com/iris-contrib/goth/examples/main.go to see this in action. +// See https://github.com/iris-contrib/gothic/blob/master/example/main.go to see this in action. func BeginAuthHandler(ctx *Context) { gothic.BeginAuthHandler(ctx) }