@@ -82,6 +82,7 @@ func server() *cobra.Command {
8282 oauth2GithubClientID string
8383 oauth2GithubClientSecret string
8484 oauth2GithubAllowedOrganizations []string
85+ oauth2GithubAllowedTeams []string
8586 oauth2GithubAllowSignups bool
8687 telemetryEnable bool
8788 telemetryURL string
@@ -264,7 +265,7 @@ func server() *cobra.Command {
264265 }
265266
266267 if oauth2GithubClientSecret != "" {
267- options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations )
268+ options .GithubOAuth2Config , err = configureGithubOAuth2 (accessURLParsed , oauth2GithubClientID , oauth2GithubClientSecret , oauth2GithubAllowSignups , oauth2GithubAllowedOrganizations , oauth2GithubAllowedTeams )
268269 if err != nil {
269270 return xerrors .Errorf ("configure github oauth2: %w" , err )
270271 }
@@ -535,6 +536,8 @@ func server() *cobra.Command {
535536 "Specifies a client secret to use for oauth2 with GitHub." )
536537 cliflag .StringArrayVarP (root .Flags (), & oauth2GithubAllowedOrganizations , "oauth2-github-allowed-orgs" , "" , "CODER_OAUTH2_GITHUB_ALLOWED_ORGS" , nil ,
537538 "Specifies organizations the user must be a member of to authenticate with GitHub." )
539+ cliflag .StringArrayVarP (root .Flags (), & oauth2GithubAllowedTeams , "oauth2-github-allowed-teams" , "" , "CODER_OAUTH2_GITHUB_ALLOWED_TEAMS" , nil ,
540+ "Specifies teams inside organizations the user must be a member of to authenticate with GitHub. Formatted as: <organization-name>/<team-slug>." )
538541 cliflag .BoolVarP (root .Flags (), & oauth2GithubAllowSignups , "oauth2-github-allow-signups" , "" , "CODER_OAUTH2_GITHUB_ALLOW_SIGNUPS" , false ,
539542 "Specifies whether new users can sign up with GitHub." )
540543 cliflag .BoolVarP (root .Flags (), & telemetryEnable , "telemetry" , "" , "CODER_TELEMETRY" , true , "Specifies whether telemetry is enabled or not. Coder collects anonymized usage data to help improve our product." )
@@ -719,11 +722,22 @@ func configureTLS(listener net.Listener, tlsMinVersion, tlsClientAuth, tlsCertFi
719722 return tls .NewListener (listener , tlsConfig ), nil
720723}
721724
722- func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string ) (* coderd.GithubOAuth2Config , error ) {
725+ func configureGithubOAuth2 (accessURL * url.URL , clientID , clientSecret string , allowSignups bool , allowOrgs []string , rawTeams [] string ) (* coderd.GithubOAuth2Config , error ) {
723726 redirectURL , err := accessURL .Parse ("/api/v2/users/oauth2/github/callback" )
724727 if err != nil {
725728 return nil , xerrors .Errorf ("parse github oauth callback url: %w" , err )
726729 }
730+ allowTeams := make ([]coderd.GithubOAuth2Team , 0 , len (rawTeams ))
731+ for _ , rawTeam := range rawTeams {
732+ parts := strings .SplitN (rawTeam , "/" , 2 )
733+ if len (parts ) != 2 {
734+ return nil , xerrors .Errorf ("github team allowlist is formatted incorrectly. got %s; wanted <organization>/<team>" , rawTeam )
735+ }
736+ allowTeams = append (allowTeams , coderd.GithubOAuth2Team {
737+ Organization : parts [0 ],
738+ Slug : parts [1 ],
739+ })
740+ }
727741 return & coderd.GithubOAuth2Config {
728742 OAuth2Config : & oauth2.Config {
729743 ClientID : clientID ,
@@ -738,6 +752,7 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
738752 },
739753 AllowSignups : allowSignups ,
740754 AllowOrganizations : allowOrgs ,
755+ AllowTeams : allowTeams ,
741756 AuthenticatedUser : func (ctx context.Context , client * http.Client ) (* github.User , error ) {
742757 user , _ , err := github .NewClient (client ).Users .Get (ctx , "" )
743758 return user , err
@@ -749,9 +764,18 @@ func configureGithubOAuth2(accessURL *url.URL, clientID, clientSecret string, al
749764 ListOrganizationMemberships : func (ctx context.Context , client * http.Client ) ([]* github.Membership , error ) {
750765 memberships , _ , err := github .NewClient (client ).Organizations .ListOrgMemberships (ctx , & github.ListOrgMembershipsOptions {
751766 State : "active" ,
767+ ListOptions : github.ListOptions {
768+ PerPage : 100 ,
769+ },
752770 })
753771 return memberships , err
754772 },
773+ ListTeams : func (ctx context.Context , client * http.Client , org string ) ([]* github.Team , error ) {
774+ teams , _ , err := github .NewClient (client ).Teams .ListTeams (ctx , org , & github.ListOptions {
775+ PerPage : 100 ,
776+ })
777+ return teams , err
778+ },
755779 }, nil
756780}
757781
0 commit comments