diff --git a/context.go b/context.go index e72b26ed..27d4cbc1 100644 --- a/context.go +++ b/context.go @@ -11,6 +11,16 @@ 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" @@ -22,15 +32,6 @@ import ( "strings" "sync" "time" - - "github.com/iris-contrib/formBinder" - "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" ) const ( @@ -781,3 +782,16 @@ func (ctx *Context) SendMail(subject string, body string, to ...string) error { func (ctx *Context) Log(format string, a ...interface{}) { ctx.framework.Logger.Printf(format, a...) } + +/* Auth */ + +// 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. +func (ctx *Context) CompleteUserAuth() (goth.User, error) { + return gothic.CompleteUserAuth(ctx) +} diff --git a/context/context.go b/context/context.go index 85670df4..fca88687 100644 --- a/context/context.go +++ b/context/context.go @@ -2,12 +2,12 @@ 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/valyala/fasthttp" ) type ( @@ -18,6 +18,7 @@ type ( IContextBinder IContextRequest IContextResponse + IContextAuth SendMail(string, string, ...string) error Log(string, ...interface{}) Reset(*fasthttp.RequestCtx) @@ -131,4 +132,16 @@ type ( Session() store.IStore SessionDestroy() } + + // 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() (goth.User, error) + } ) diff --git a/iris.go b/iris.go index f7d7bb51..89180e26 100644 --- a/iris.go +++ b/iris.go @@ -59,10 +59,10 @@ import ( "strings" "time" - "github.com/kataras/iris/errors" - + "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/utils" "github.com/valyala/fasthttp" ) @@ -568,6 +568,18 @@ func (s *Framework) TemplateString(templateFile string, pageContext interface{}, return res } +// 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". +// +// 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. +func BeginAuthHandler(ctx *Context) { + gothic.BeginAuthHandler(ctx) +} + // ------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------- // ----------------------------------MuxAPI implementation------------------------------