code.gitea.io/gitea@v1.21.7/services/auth/source/oauth2/providers_simple.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package oauth2
     5  
     6  import (
     7  	"code.gitea.io/gitea/modules/setting"
     8  
     9  	"github.com/markbates/goth"
    10  	"github.com/markbates/goth/providers/azuread"
    11  	"github.com/markbates/goth/providers/bitbucket"
    12  	"github.com/markbates/goth/providers/discord"
    13  	"github.com/markbates/goth/providers/dropbox"
    14  	"github.com/markbates/goth/providers/facebook"
    15  	"github.com/markbates/goth/providers/google"
    16  	"github.com/markbates/goth/providers/microsoftonline"
    17  	"github.com/markbates/goth/providers/twitter"
    18  	"github.com/markbates/goth/providers/yandex"
    19  )
    20  
    21  // SimpleProviderNewFn create goth.Providers without custom url features
    22  type SimpleProviderNewFn func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider
    23  
    24  // SimpleProvider is a GothProvider which does not have custom url features
    25  type SimpleProvider struct {
    26  	BaseProvider
    27  	scopes []string
    28  	newFn  SimpleProviderNewFn
    29  }
    30  
    31  // CreateGothProvider creates a GothProvider from this Provider
    32  func (c *SimpleProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
    33  	scopes := make([]string, len(c.scopes)+len(source.Scopes))
    34  	copy(scopes, c.scopes)
    35  	copy(scopes[len(c.scopes):], source.Scopes)
    36  	return c.newFn(source.ClientID, source.ClientSecret, callbackURL, scopes...), nil
    37  }
    38  
    39  // NewSimpleProvider is a constructor function for simple providers
    40  func NewSimpleProvider(name, displayName string, scopes []string, newFn SimpleProviderNewFn) *SimpleProvider {
    41  	return &SimpleProvider{
    42  		BaseProvider: BaseProvider{
    43  			name:        name,
    44  			displayName: displayName,
    45  		},
    46  		scopes: scopes,
    47  		newFn:  newFn,
    48  	}
    49  }
    50  
    51  var _ GothProvider = &SimpleProvider{}
    52  
    53  func init() {
    54  	RegisterGothProvider(
    55  		NewSimpleProvider("bitbucket", "Bitbucket", []string{"account"},
    56  			func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    57  				return bitbucket.New(clientKey, secret, callbackURL, scopes...)
    58  			}))
    59  
    60  	RegisterGothProvider(
    61  		NewSimpleProvider("dropbox", "Dropbox", nil,
    62  			func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    63  				return dropbox.New(clientKey, secret, callbackURL, scopes...)
    64  			}))
    65  
    66  	RegisterGothProvider(NewSimpleProvider("facebook", "Facebook", nil,
    67  		func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    68  			return facebook.New(clientKey, secret, callbackURL, scopes...)
    69  		}))
    70  
    71  	// named gplus due to legacy gplus -> google migration (Google killed Google+). This ensures old connections still work
    72  	RegisterGothProvider(NewSimpleProvider("gplus", "Google", []string{"email"},
    73  		func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    74  			if setting.OAuth2Client.UpdateAvatar || setting.OAuth2Client.EnableAutoRegistration {
    75  				scopes = append(scopes, "profile")
    76  			}
    77  			return google.New(clientKey, secret, callbackURL, scopes...)
    78  		}))
    79  
    80  	RegisterGothProvider(NewSimpleProvider("twitter", "Twitter", nil,
    81  		func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    82  			return twitter.New(clientKey, secret, callbackURL)
    83  		}))
    84  
    85  	RegisterGothProvider(NewSimpleProvider("discord", "Discord", []string{discord.ScopeIdentify, discord.ScopeEmail},
    86  		func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    87  			return discord.New(clientKey, secret, callbackURL, scopes...)
    88  		}))
    89  
    90  	// See https://tech.yandex.com/passport/doc/dg/reference/response-docpage/
    91  	RegisterGothProvider(NewSimpleProvider("yandex", "Yandex", []string{"login:email", "login:info", "login:avatar"},
    92  		func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
    93  			return yandex.New(clientKey, secret, callbackURL, scopes...)
    94  		}))
    95  
    96  	RegisterGothProvider(NewSimpleProvider(
    97  		"azuread", "Azure AD", nil,
    98  		func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
    99  			return azuread.New(clientID, secret, callbackURL, nil, scopes...)
   100  		},
   101  	))
   102  
   103  	RegisterGothProvider(NewSimpleProvider(
   104  		"microsoftonline", "Microsoft Online", nil,
   105  		func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
   106  			return microsoftonline.New(clientID, secret, callbackURL, scopes...)
   107  		},
   108  	))
   109  }