@@ -87,6 +87,7 @@ func server() *cobra.Command {
87
87
oauth2GithubAllowedOrganizations []string
88
88
oauth2GithubAllowedTeams []string
89
89
oauth2GithubAllowSignups bool
90
+ oauth2GithubEnterpriseBaseURL string
90
91
oidcAllowSignups bool
91
92
oidcClientID string
92
93
oidcClientSecret string
@@ -286,7 +287,7 @@ func server() *cobra.Command {
286
287
}
287
288
288
289
if oauth2GithubClientSecret != "" {
289
- options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations , oauth2GithubAllowedTeams )
290
+ options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations , oauth2GithubAllowedTeams , oauth2GithubEnterpriseBaseURL )
290
291
if err != nil {
291
292
return xerrors .Errorf ("configure github oauth2: %w" , err )
292
293
}
@@ -695,6 +696,8 @@ func server() *cobra.Command {
695
696
"Specifies teams inside organizations the user must be a member of to authenticate with GitHub. Formatted as: <organization-name>/<team-slug>." )
696
697
cliflag .BoolVarP (root .Flags (), & oauth2GithubAllowSignups , "oauth2-github-allow-signups" , "" , "CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS" , false ,
697
698
"Specifies whether new users can sign up with GitHub." )
699
+ cliflag .StringVarP (root .Flags (), & oauth2GithubEnterpriseBaseURL , "oauth2-github-enterprise-base-url" , "" , "CODER_OAUTH2_GITHUB_ENTERPRISE_BASE_URL" , "" ,
700
+ "Specifies the base URL of a GitHub Enterprise instance to use for oauth2." )
698
701
cliflag .BoolVarP (root .Flags (), & oidcAllowSignups , "oidc-allow-signups" , "" , "CODER_OIDC_ALLOW_SIGNUPS" , true ,
699
702
"Specifies whether new users can sign up with OIDC." )
700
703
cliflag .StringVarP (root .Flags (), & oidcClientID , "oidc-client-id" , "" , "CODER_OIDC_CLIENT_ID" , "" ,
@@ -972,7 +975,7 @@ func configureTLS(listener net.Listener, tlsMinVersion, tlsClientAuth, tlsCertFi
972
975
return tls .NewListener (listener , tlsConfig ), nil
973
976
}
974
977
975
- func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string , rawTeams []string ) (* coderd.GithubOAuth2Config , error ) {
978
+ func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string , rawTeams []string , enterpriseBaseURL string ) (* coderd.GithubOAuth2Config , error ) {
976
979
redirectURL , err := accessURL .Parse ("/api/v2/users/oauth2/github/callback" )
977
980
if err != nil {
978
981
return nil , xerrors .Errorf ("parse github oauth callback url: %w" , err )
@@ -988,11 +991,38 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
988
991
Slug : parts [1 ],
989
992
})
990
993
}
994
+ createClient := func (client * http.Client ) (* github.Client , error ) {
995
+ if enterpriseBaseURL != "" {
996
+ return github .NewEnterpriseClient (enterpriseBaseURL , "" , client )
997
+ }
998
+ return github .NewClient (client ), nil
999
+ }
1000
+
1001
+ endpoint := xgithub .Endpoint
1002
+ if enterpriseBaseURL != "" {
1003
+ enterpriseURL , err := url .Parse (enterpriseBaseURL )
1004
+ if err != nil {
1005
+ return nil , xerrors .Errorf ("parse enterprise base url: %w" , err )
1006
+ }
1007
+ authURL , err := enterpriseURL .Parse ("/login/oauth/authorize" )
1008
+ if err != nil {
1009
+ return nil , xerrors .Errorf ("parse enterprise auth url: %w" , err )
1010
+ }
1011
+ tokenURL , err := enterpriseURL .Parse ("/login/oauth/access_token" )
1012
+ if err != nil {
1013
+ return nil , xerrors .Errorf ("parse enterprise token url: %w" , err )
1014
+ }
1015
+ endpoint = oauth2.Endpoint {
1016
+ AuthURL : authURL .String (),
1017
+ TokenURL : tokenURL .String (),
1018
+ }
1019
+ }
1020
+
991
1021
return & coderd.GithubOAuth2Config {
992
1022
OAuth2Config : & oauth2.Config {
993
1023
ClientID : clientID ,
994
1024
ClientSecret : clientSecret ,
995
- Endpoint : xgithub . Endpoint ,
1025
+ Endpoint : endpoint ,
996
1026
RedirectURL : redirectURL .String (),
997
1027
Scopes : []string {
998
1028
"read:user" ,
@@ -1004,15 +1034,27 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
1004
1034
AllowOrganizations : allowOrgs ,
1005
1035
AllowTeams : allowTeams ,
1006
1036
AuthenticatedUser : func (ctx context.Context , client * http.Client ) (* github.User , error ) {
1007
- user , _ , err := github .NewClient (client ).Users .Get (ctx , "" )
1037
+ api , err := createClient (client )
1038
+ if err != nil {
1039
+ return nil , err
1040
+ }
1041
+ user , _ , err := api .Users .Get (ctx , "" )
1008
1042
return user , err
1009
1043
},
1010
1044
ListEmails : func (ctx context.Context , client * http.Client ) ([]* github.UserEmail , error ) {
1011
- emails , _ , err := github .NewClient (client ).Users .ListEmails (ctx , & github.ListOptions {})
1045
+ api , err := createClient (client )
1046
+ if err != nil {
1047
+ return nil , err
1048
+ }
1049
+ emails , _ , err := api .Users .ListEmails (ctx , & github.ListOptions {})
1012
1050
return emails , err
1013
1051
},
1014
1052
ListOrganizationMemberships : func (ctx context.Context , client * http.Client ) ([]* github.Membership , error ) {
1015
- memberships , _ , err := github .NewClient (client ).Organizations .ListOrgMemberships (ctx , & github.ListOrgMembershipsOptions {
1053
+ api , err := createClient (client )
1054
+ if err != nil {
1055
+ return nil , err
1056
+ }
1057
+ memberships , _ , err := api .Organizations .ListOrgMemberships (ctx , & github.ListOrgMembershipsOptions {
1016
1058
State : "active" ,
1017
1059
ListOptions : github.ListOptions {
1018
1060
PerPage : 100 ,
@@ -1021,7 +1063,11 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
1021
1063
return memberships , err
1022
1064
},
1023
1065
TeamMembership : func (ctx context.Context , client * http.Client , org , teamSlug , username string ) (* github.Membership , error ) {
1024
- team , _ , err := github .NewClient (client ).Teams .GetTeamMembershipBySlug (ctx , org , teamSlug , username )
1066
+ api , err := createClient (client )
1067
+ if err != nil {
1068
+ return nil , err
1069
+ }
1070
+ team , _ , err := api .Teams .GetTeamMembershipBySlug (ctx , org , teamSlug , username )
1025
1071
return team , err
1026
1072
},
1027
1073
}, nil
0 commit comments