mirror of
https://github.com/wavetermdev/wails.git
synced 2026-08-05 13:53:43 -07:00
[v3] Add oauth plugin
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
# oauth Plugin
|
||||
|
||||
This plugin provides the ability to initiate an OAuth authentication flow.
|
||||
|
||||
## Installation
|
||||
|
||||
Add the plugin to the `Plugins` option in the Applications options:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"github.com/wailsapp/wails/v3/plugins/browser"
|
||||
)
|
||||
|
||||
func main() {
|
||||
oAuthPlugin := oauth.NewPlugin(oauth.Config{
|
||||
Providers: []goth.Provider{
|
||||
github.New(
|
||||
os.Getenv("clientkey"),
|
||||
os.Getenv("secret"),
|
||||
"http://localhost:9876/auth/github/callback",
|
||||
"email",
|
||||
"profile"),
|
||||
},
|
||||
})
|
||||
|
||||
app := application.New(application.Options{
|
||||
// ...
|
||||
Plugins: map[string]application.Plugin{
|
||||
"oauth": oAuthPlugin,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Go
|
||||
|
||||
You can start the flow by calling `Start()` on the plugin instance:
|
||||
|
||||
```go
|
||||
app.Events.On("github-login", func(e *application.WailsEvent) {
|
||||
oAuthPlugin.Start()
|
||||
oAuthWindow.Show()
|
||||
})
|
||||
```
|
||||
|
||||
There is a working example of github auth in the `v3/examples` directory.
|
||||
|
||||
## Support
|
||||
|
||||
If you find a bug in this plugin, please raise a ticket on the Wails [Issue Tracker](https://github.com/wailsapp/wails/issues).
|
||||
@@ -0,0 +1,135 @@
|
||||
package oauth
|
||||
|
||||
import (
|
||||
"github.com/gorilla/pat"
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/markbates/goth"
|
||||
"github.com/markbates/goth/gothic"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ---------------- Plugin Setup ----------------
|
||||
// This is the main plugin struct. It can be named anything you like.
|
||||
// It must implement the application.Plugin interface.
|
||||
// Both the Init() and Shutdown() methods are called synchronously when the app starts and stops.
|
||||
|
||||
const (
|
||||
Success = "wails:oauth:success"
|
||||
Error = "wails:oauth:error"
|
||||
)
|
||||
|
||||
type Plugin struct {
|
||||
config Config
|
||||
server *http.Server
|
||||
router *pat.Router
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
|
||||
// Address to bind the temporary webserver to
|
||||
// Defaults to localhost:9876
|
||||
Address string
|
||||
|
||||
// SessionSecret is the secret used to encrypt the session store.
|
||||
SessionSecret string
|
||||
|
||||
// MaxAge is the maximum age of the session in seconds.
|
||||
MaxAge int
|
||||
|
||||
// Providers is a list of goth providers to use.
|
||||
Providers []goth.Provider
|
||||
}
|
||||
|
||||
func NewPlugin(config Config) *Plugin {
|
||||
result := &Plugin{
|
||||
config: config,
|
||||
}
|
||||
if result.config.MaxAge == 0 {
|
||||
result.config.MaxAge = 86400 * 30 // 30 days
|
||||
}
|
||||
if result.config.Address == "" {
|
||||
result.config.Address = "localhost:9876"
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (p *Plugin) Shutdown() {
|
||||
if p.server != nil {
|
||||
p.server.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) Name() string {
|
||||
return "github.com/wailsapp/wails/v3/plugins/oauth"
|
||||
}
|
||||
|
||||
func (p *Plugin) Init(_ *application.App) error {
|
||||
|
||||
store := sessions.NewCookieStore([]byte(p.config.SessionSecret))
|
||||
store.MaxAge(p.config.MaxAge)
|
||||
store.Options.Path = "/"
|
||||
store.Options.HttpOnly = true
|
||||
store.Options.Secure = false
|
||||
|
||||
gothic.Store = store
|
||||
goth.UseProviders(p.config.Providers...)
|
||||
|
||||
p.router = pat.New()
|
||||
p.router.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) {
|
||||
|
||||
println("Callback...")
|
||||
event := &application.WailsEvent{
|
||||
Name: Success,
|
||||
Sender: "",
|
||||
}
|
||||
user, err := gothic.CompleteUserAuth(res, req)
|
||||
if err != nil {
|
||||
event.Data = err.Error()
|
||||
event.Name = Error
|
||||
} else {
|
||||
event.Data = user
|
||||
}
|
||||
application.Get().Events.Emit(event)
|
||||
err = p.server.Close()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
p.server = nil
|
||||
})
|
||||
|
||||
p.router.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) {
|
||||
println("Authenticating...")
|
||||
gothic.BeginAuthHandler(res, req)
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Plugin) CallableByJS() []string {
|
||||
return []string{
|
||||
"Start",
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Plugin) InjectJS() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// ---------------- Plugin Methods ----------------
|
||||
|
||||
func (p *Plugin) Start() {
|
||||
if p.server != nil {
|
||||
println("Already listening")
|
||||
return
|
||||
}
|
||||
p.server = &http.Server{
|
||||
Addr: p.config.Address,
|
||||
Handler: p.router,
|
||||
}
|
||||
println("Starting server")
|
||||
|
||||
go p.server.ListenAndServe()
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
/**
|
||||
* Starts the oauth temporary server.
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function Start() {
|
||||
return wails.Plugin("oauth", "Start");
|
||||
}
|
||||
|
||||
export default {
|
||||
OAuth: {
|
||||
Start,
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,11 @@
|
||||
# This is the plugin definition file for the "oauth" plugin.
|
||||
|
||||
Name = "oauth"
|
||||
Description = "Provides oauth capabilities."
|
||||
Author = "Lea Anthony"
|
||||
Version = "v1.0.0"
|
||||
Website = "https://wails.io"
|
||||
Repository = "https://github.com/wailsapp/wails/v3/plugins/oauth"
|
||||
License = "MIT"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user