Open platform to collect and prioritize feedback
add github oauth (#86)
Guilherme Oenning21095d679e85
diff --git a/.vscode/settings.json b/.vscode/settings.jsonindex 8a1f45edb..cd5720912 100644--- a/.vscode/settings.json+++ b/.vscode/settings.json@@ -31,5 +31,6 @@"go.coverOnSave": true,"go.coverageOptions": "showBothCoveredAndUncoveredCode","go.coverageDecorator": "gutter",- "go.testEnvFile": "${workspaceRoot}/.test.env"+ "go.testEnvFile": "${workspaceRoot}/.test.env",+ "go.lintTool": "golint"}\ No newline at end of filediff --git a/app/handlers/oauth.go b/app/handlers/oauth.goindex ba29a0b1f..347e81259 100644--- a/app/handlers/oauth.go+++ b/app/handlers/oauth.go@@ -61,7 +61,7 @@ func OAuthCallback(provider string) web.HandlerFunc {var user *models.Userif oauthUser.Email == "" {- user, err = users.GetByProvider(tenant.ID, provider, oauthUser.ID)+ user, err = users.GetByProvider(tenant.ID, provider, oauthUser.ID.String())} else {user, err = users.GetByEmail(tenant.ID, oauthUser.Email)}@@ -74,7 +74,7 @@ func OAuthCallback(provider string) web.HandlerFunc {Role: models.RoleVisitor,Providers: []*models.UserProvider{&models.UserProvider{- UID: oauthUser.ID,+ UID: oauthUser.ID.String(),Name: provider,},},@@ -89,7 +89,7 @@ func OAuthCallback(provider string) web.HandlerFunc {}} else if !user.HasProvider(provider) {err = users.RegisterProvider(user.ID, &models.UserProvider{- UID: oauthUser.ID,+ UID: oauthUser.ID.String(),Name: provider,})if err != nil {@@ -104,7 +104,7 @@ func OAuthCallback(provider string) web.HandlerFunc {}} else {claims = models.OAuthClaims{- OAuthID: oauthUser.ID,+ OAuthID: oauthUser.ID.String(),OAuthProvider: provider,OAuthName: oauthUser.Name,OAuthEmail: oauthUser.Email,diff --git a/app/pkg/oauth/http.go b/app/pkg/oauth/http.goindex 50ce3c9f4..0e0efdf62 100644--- a/app/pkg/oauth/http.go+++ b/app/pkg/oauth/http.go@@ -11,6 +11,7 @@ import ("golang.org/x/oauth2""golang.org/x/oauth2/facebook"+ "golang.org/x/oauth2/github""golang.org/x/oauth2/google")@@ -52,6 +53,22 @@ var (}},},+ GitHubProvider: {+ profileURL: func(token *oauth2.Token) string {+ return "https://api.github.com/user?access_token=" + url.QueryEscape(token.AccessToken)+ },+ config: func(authEndpoint string) *oauth2.Config {+ return &oauth2.Config{+ ClientID: os.Getenv("OAUTH_GITHUB_CLIENTID"),+ ClientSecret: os.Getenv("OAUTH_GITHUB_SECRET"),+ RedirectURL: authEndpoint + "/oauth/github/callback",+ Scopes: []string{+ "user:email",+ },+ Endpoint: github.Endpoint,+ }+ },+ },})diff --git a/app/pkg/oauth/oauth.go b/app/pkg/oauth/oauth.goindex 384bd7eca..8c54c1325 100644--- a/app/pkg/oauth/oauth.go+++ b/app/pkg/oauth/oauth.go@@ -1,6 +1,7 @@package oauthimport (+ "encoding/json""os")@@ -9,11 +10,13 @@ const (FacebookProvider = "facebook"//GoogleProvider is const for 'google'GoogleProvider = "google"+ //GitHubProvider is const for 'github'+ GitHubProvider = "github")//UserProfile represents an OAuth user profiletype UserProfile struct {- ID string+ ID json.NumberName stringEmail string}@@ -24,6 +27,8 @@ func IsProviderEnabled(name string) bool {return os.Getenv("OAUTH_GOOGLE_CLIENTID") != ""} else if name == FacebookProvider {return os.Getenv("OAUTH_FACEBOOK_APPID") != ""+ } else if name == GitHubProvider {+ return os.Getenv("OAUTH_GITHUB_CLIENTID") != ""}return false}diff --git a/app/pkg/web/renderer.go b/app/pkg/web/renderer.goindex 81699c722..ad71ccc24 100644--- a/app/pkg/web/renderer.go+++ b/app/pkg/web/renderer.go@@ -66,6 +66,7 @@ func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo"providers": Map{oauth.GoogleProvider: oauth.IsProviderEnabled(oauth.GoogleProvider),oauth.FacebookProvider: oauth.IsProviderEnabled(oauth.FacebookProvider),+ oauth.GitHubProvider: oauth.IsProviderEnabled(oauth.GitHubProvider),},}m["settings"] = r.settingsdiff --git a/docs/docker-compose.yml b/docs/docker-compose.ymlindex 86f94006a..bfd2ceb30 100644--- a/docs/docker-compose.yml+++ b/docs/docker-compose.yml@@ -34,9 +34,13 @@ services:# OAUTH_FACEBOOK_APPID: <fb_app_id># OAUTH_FACEBOOK_SECRET: <fb_app_secret>+ # GitHub: https://github.com/settings/applications/new+ # OAUTH_GITHUB_APPID: <github_client_id>+ # OAUTH_GITHUB_SECRET: <github_secret>+# Google: https://console.developers.google.com- # OAUTH_GOOGLE_CLIENTID: <google_app_id>- # OAUTH_GOOGLE_SECRET: <google_app_secret>+ # OAUTH_GOOGLE_CLIENTID: <google_client_id>+ # OAUTH_GOOGLE_SECRET: <google_secret>depends_on:- db\ No newline at end of filediff --git a/public/components/common/SocialSignInButton.tsx b/public/components/common/SocialSignInButton.tsxindex 28895baf6..f97895ba4 100644--- a/public/components/common/SocialSignInButton.tsx+++ b/public/components/common/SocialSignInButton.tsx@@ -5,10 +5,28 @@ import { Session } from '@fider/services/Session';import { Button } from '@fider/components/common';interface SocialSignInButtonProps {- provider: string;+ provider: 'google' | 'facebook' | 'github';size: 'small' | 'normal';}+const providers = {+ google: {+ name: 'Google',+ class: 'google plus',+ icon: 'google plus'+ },+ facebook: {+ name: 'Facebook',+ class: 'facebook',+ icon: 'facebook'+ },+ github: {+ name: 'GitHub',+ class: 'github black',+ icon: 'github'+ }+};+export class SocialSignInButton extends React.Component<SocialSignInButtonProps, {}> {@inject(injectables.Session)@@ -16,14 +34,13 @@ export class SocialSignInButton extends React.Component<SocialSignInButtonProps,public render() {const auth = this.session.get<any>('auth');- const providerClassName = this.props.provider === 'google' ? 'google plus' : 'facebook';- const providerDisplayName = this.props.provider === 'google' ? 'Google' : 'Facebook';+const href = `${auth.endpoint}/oauth/${this.props.provider}?redirect=${location.href}`;- const classes = `${providerClassName} ${this.props.size === 'small' ? 'circular icon' : 'fluid'}`;+ const classes = `${providers[this.props.provider].class} ${this.props.size === 'small' ? 'circular icon' : 'fluid'}`;return <Button href={href} className={classes}>- <i className={providerClassName + ' icon'}></i>- { this.props.size === 'normal' && `Sign in with ${providerDisplayName}` }+ <i className={'icon ' + providers[this.props.provider].icon}></i>+ { this.props.size === 'normal' && `Sign in with ${providers[this.props.provider].name}` }</Button>;}}diff --git a/public/components/common/SocialSignInList.tsx b/public/components/common/SocialSignInList.tsxindex 3f2ba1231..9acb69d57 100644--- a/public/components/common/SocialSignInList.tsx+++ b/public/components/common/SocialSignInList.tsx@@ -7,7 +7,8 @@ interface AuthSettings {endpoint: string;providers: {google: boolean,- facebook: boolean+ facebook: boolean,+ github: boolean};}@@ -37,6 +38,10 @@ export class SocialSignInList extends React.Component<SocialSignInListProps, {}><div className="item"><SocialSignInButton provider="facebook" size={this.props.size} /></div>;+ const github = settings.providers.github &&+ <div className="item">+ <SocialSignInButton provider="github" size={this.props.size} />+ </div>;const noAuth = !facebook && !google &&<div className="item">@@ -48,6 +53,7 @@ export class SocialSignInList extends React.Component<SocialSignInListProps, {}>return <div className={`ui list signin-list ${cssClasses}`}>{ facebook }{ google }+ { github }{ noAuth }</div>;}diff --git a/router.go b/router.goindex 24f19dd81..14c513392 100644--- a/router.go+++ b/router.go@@ -55,6 +55,8 @@ func GetMainEngine(settings *models.AppSettings) *web.Engine {auth.Get("/facebook/callback", handlers.OAuthCallback(oauth.FacebookProvider))auth.Get("/google", handlers.Login(oauth.GoogleProvider))auth.Get("/google/callback", handlers.OAuthCallback(oauth.GoogleProvider))+ auth.Get("/github", handlers.Login(oauth.GitHubProvider))+ auth.Get("/github/callback", handlers.OAuthCallback(oauth.GitHubProvider))}page := r.Group("")