Browse Source

Aded show only the maintained oAuth providers in the frontend. Fix #41

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 8 years ago
parent
commit
0d7a5e49e0
  1. 16
      handlers/auth.go
  2. 7
      handlers/handlers.go
  3. 4
      handlers/public.go
  4. 56
      static/src/index.js

16
handlers/auth.go

@ -17,9 +17,19 @@ import (
func (h *Handler) initOAuth() { func (h *Handler) initOAuth() {
h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey()))) h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey())))
auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("Google.ClientID"), viper.GetString("Google.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/google")) h.providers = []string{}
auth.WithAdapterWrapper(auth.NewGithubAdapter(viper.GetString("GitHub.ClientID"), viper.GetString("GitHub.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/github")) if viper.GetString("Google.ClientSecret") != "" {
auth.WithAdapterWrapper(auth.NewMicrosoftAdapter(viper.GetString("Microsoft.ClientID"), viper.GetString("Microsoft.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/microsoft")) auth.WithAdapterWrapper(auth.NewGoogleAdapter(viper.GetString("Google.ClientID"), viper.GetString("Google.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/google"))
h.providers = append(h.providers, "google")
}
if viper.GetString("GitHub.ClientSecret") != "" {
auth.WithAdapterWrapper(auth.NewGithubAdapter(viper.GetString("GitHub.ClientID"), viper.GetString("GitHub.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/github"))
h.providers = append(h.providers, "github")
}
if viper.GetString("Microsoft.ClientSecret") != "" {
auth.WithAdapterWrapper(auth.NewMicrosoftAdapter(viper.GetString("Microsoft.ClientID"), viper.GetString("Microsoft.ClientSecret"), viper.GetString("base_url")), h.engine.Group("/api/v1/auth/microsoft"))
h.providers = append(h.providers, "microsoft")
}
h.engine.POST("/api/v1/check", h.handleAuthCheck) h.engine.POST("/api/v1/check", h.handleAuthCheck)
} }

7
handlers/handlers.go

@ -20,8 +20,9 @@ import (
// Handler holds the funcs and attributes for the // Handler holds the funcs and attributes for the
// http communication // http communication
type Handler struct { type Handler struct {
store store.Store store store.Store
engine *gin.Engine engine *gin.Engine
providers []string
} }
// DoNotPrivateKeyChecking is used for testing // DoNotPrivateKeyChecking is used for testing
@ -71,6 +72,8 @@ func (h *Handler) setHandlers() error {
protected.POST("/create", h.handleCreate) protected.POST("/create", h.handleCreate)
protected.POST("/lookup", h.handleLookup) protected.POST("/lookup", h.handleLookup)
h.engine.GET("/api/v1/info", h.handleInfo)
h.engine.NoRoute(h.handleAccess, gin.WrapH(http.FileServer(FS(false)))) h.engine.NoRoute(h.handleAccess, gin.WrapH(http.FileServer(FS(false))))
return nil return nil
} }

4
handlers/public.go

@ -86,6 +86,10 @@ func (h *Handler) handleCreate(c *gin.Context) {
c.JSON(http.StatusOK, data) c.JSON(http.StatusOK, data)
} }
func (h *Handler) handleInfo(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"providers": h.providers})
}
func (h *Handler) getSchemaAndHost(c *gin.Context) string { func (h *Handler) getSchemaAndHost(c *gin.Context) string {
protocol := "http" protocol := "http"
if c.Request.TLS != nil { if c.Request.TLS != nil {

56
static/src/index.js

@ -14,7 +14,8 @@ export default class BaseComponent extends Component {
open: true, open: true,
userData: {}, userData: {},
authorized: false, authorized: false,
activeItem: "" activeItem: "",
providers: []
} }
onOAuthClose() { onOAuthClose() {
@ -25,6 +26,11 @@ export default class BaseComponent extends Component {
componentWillMount() { componentWillMount() {
this.checkAuth() this.checkAuth()
this.loadInfo()
}
loadInfo = () => {
fetch('/api/v1/info').then(d => d.json()).then(d => this.setState({ providers: d.providers }))
} }
checkAuth = () => { checkAuth = () => {
@ -61,12 +67,17 @@ export default class BaseComponent extends Component {
} }
onOAuthClick = provider => { onOAuthClick = provider => {
window.addEventListener('message', this.onOAuthCallback, false); window.addEventListener('message', this.onOAuthCallback, false);
// Open the oAuth window that is it centered in the middle of the screen var url = `${window.location.origin}/api/v1/auth/${provider}/login`;
var wwidth = 400, if (!this._oAuthPopup) {
wHeight = 500; // Open the oAuth window that is it centered in the middle of the screen
var wLeft = (window.screen.width / 2) - (wwidth / 2); var wwidth = 400,
var wTop = (window.screen.height / 2) - (wHeight / 2); wHeight = 500;
window.open(`/api/v1/auth/${provider}/login`, '', `width=${wwidth}, height=${wHeight}, top=${wTop}, left=${wLeft}`) var wLeft = (window.screen.width / 2) - (wwidth / 2);
var wTop = (window.screen.height / 2) - (wHeight / 2);
this._oAuthPopup = window.open(url, '', `width=${wwidth}, height=${wHeight}, top=${wTop}, left=${wLeft}`)
} else {
this._oAuthPopup.location = url;
}
} }
handleLogout = () => { handleLogout = () => {
@ -75,7 +86,7 @@ export default class BaseComponent extends Component {
} }
render() { render() {
const { open, authorized, activeItem, userData } = this.state const { open, authorized, activeItem, userData, providers } = this.state
if (!authorized) { if (!authorized) {
return ( return (
<Modal size='tiny' open={open} onClose={this.onOAuthClose}> <Modal size='tiny' open={open} onClose={this.onOAuthClose}>
@ -85,17 +96,24 @@ export default class BaseComponent extends Component {
<Modal.Content> <Modal.Content>
<p>The following authentication services are currently available:</p> <p>The following authentication services are currently available:</p>
<div className='ui center aligned segment'> <div className='ui center aligned segment'>
<Button className='ui google plus button' onClick={this.onOAuthClick.bind(this, "google")}> {providers.length == 0 && <p>There are currently no correct oAuth credential maintained.</p>}
<Icon name='google' /> Login with Google {providers.indexOf("microsoft") != -1 && <div>
</Button> <Button className='ui google plus button' onClick={this.onOAuthClick.bind(this, "google")}>
<div className="ui divider"></div> <Icon name='google' /> Login with Google
<Button style={{ backgroundColor: "#333", color: "white" }} onClick={this.onOAuthClick.bind(this, "github")}> </Button>
<Icon name='github' /> Login with GitHub </div>}
</Button> {providers.indexOf("microsoft") != -1 && <div>
<div className="ui divider"></div> <div className="ui divider"></div>
<Button style={{ backgroundColor: "#0067b8", color: "white" }} onClick={this.onOAuthClick.bind(this, "microsoft")}> <Button style={{ backgroundColor: "#333", color: "white" }} onClick={this.onOAuthClick.bind(this, "github")}>
<Icon name='windows' /> Login with Microsoft <Icon name='github' /> Login with GitHub
</Button> </Button>
</div>}
{providers.indexOf("microsoft") != -1 && <div>
<div className="ui divider"></div>
<Button style={{ backgroundColor: "#0067b8", color: "white" }} onClick={this.onOAuthClick.bind(this, "microsoft")}>
<Icon name='windows' /> Login with Microsoft
</Button>
</div>}
</div> </div>
</Modal.Content> </Modal.Content>
</Modal> </Modal>

Loading…
Cancel
Save