Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0af6a37

Browse files
committed
Redesign the git auth page
1 parent 16ce6e1 commit 0af6a37

File tree

18 files changed

+400
-71
lines changed

18 files changed

+400
-71
lines changed

cli/server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func ReadGitAuthProvidersFromEnv(environ []string) ([]codersdk.GitAuthConfig, er
164164
provider.NoRefresh = b
165165
case "SCOPES":
166166
provider.Scopes = strings.Split(v.Value, " ")
167+
case "APP_INSTALL_URL":
168+
provider.AppInstallURL = v.Value
169+
case "APP_INSTALLATIONS_URL":
170+
provider.AppInstallationsURL = v.Value
167171
}
168172
providers[providerNum] = provider
169173
}

coderd/apidoc/docs.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/gitauth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func (api *API) gitAuthByID(w http.ResponseWriter, r *http.Request) {
2424
res := codersdk.GitAuth{
2525
Authenticated: false,
2626
Device: config.DeviceAuth != nil,
27+
AppInstallURL: config.AppInstallURL,
28+
Type: config.Type.Pretty(),
2729
}
2830

2931
link, err := api.Database.GetGitAuthLink(ctx, database.GetGitAuthLinkParams{
@@ -37,7 +39,7 @@ func (api *API) gitAuthByID(w http.ResponseWriter, r *http.Request) {
3739
return err
3840
})
3941
eg.Go(func() (err error) {
40-
res.AppInstallations, err = config.AppInstallations(ctx, link.OAuthAccessToken)
42+
res.AppInstallations, res.AppInstallable, err = config.AppInstallations(ctx, link.OAuthAccessToken)
4143
return err
4244
})
4345
err = eg.Wait()

coderd/gitauth/config.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type Config struct {
4444
// returning it to the user. If omitted, tokens will
4545
// not be validated before being returned.
4646
ValidateURL string
47-
// InstallURL is for GitHub App's (and hopefully others eventually)
47+
// AppInstallURL is for GitHub App's (and hopefully others eventually)
4848
// to provide a link to install the app. There's installation
4949
// of the application, and user authentication. It's possible
5050
// for the user to authenticate but the application to not.
@@ -155,29 +155,31 @@ type AppInstallation struct {
155155

156156
// AppInstallations returns a list of app installations for the given token.
157157
// If the provider does not support app installations, it returns nil.
158-
func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk.GitAuthAppInstallation, error) {
158+
func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk.GitAuthAppInstallation, bool, error) {
159159
if c.AppInstallationsURL == "" {
160-
return nil, nil
160+
return nil, false, nil
161161
}
162162
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.AppInstallationsURL, nil)
163163
if err != nil {
164-
return nil, err
164+
return nil, false, err
165165
}
166166
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
167167
res, err := http.DefaultClient.Do(req)
168168
if err != nil {
169-
return nil, err
169+
return nil, false, err
170170
}
171171
defer res.Body.Close()
172172

173173
installs := []codersdk.GitAuthAppInstallation{}
174174
if c.Type == codersdk.GitProviderGitHub {
175-
var ghInstalls []github.Installation
175+
var ghInstalls struct {
176+
Installations []*github.Installation `json:"installations"`
177+
}
176178
err = json.NewDecoder(res.Body).Decode(&ghInstalls)
177179
if err != nil {
178-
return nil, err
180+
return nil, false, err
179181
}
180-
for _, installation := range ghInstalls {
182+
for _, installation := range ghInstalls.Installations {
181183
install := codersdk.GitAuthAppInstallation{
182184
ID: int(installation.GetID()),
183185
ConfigureURL: installation.GetHTMLURL(),
@@ -194,7 +196,7 @@ func (c *Config) AppInstallations(ctx context.Context, token string) ([]codersdk
194196
installs = append(installs, install)
195197
}
196198
}
197-
return installs, nil
199+
return installs, true, nil
198200
}
199201

200202
// ConvertConfig converts the SDK configuration entry format
@@ -301,7 +303,7 @@ func ConvertConfig(entries []codersdk.GitAuthConfig, accessURL *url.URL) ([]*Con
301303
}
302304
cfg.DeviceAuth = &DeviceAuth{
303305
ClientID: entry.ClientID,
304-
TokenURL: entry.TokenURL,
306+
TokenURL: oc.Endpoint.TokenURL,
305307
Scopes: entry.Scopes,
306308
CodeURL: entry.DeviceAuthURL,
307309
}

codersdk/gitauth.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ import (
88
)
99

1010
type GitAuth struct {
11-
Authenticated bool `json:"authenticated"`
12-
Device bool `json:"device"`
11+
Authenticated bool `json:"authenticated"`
12+
Device bool `json:"device"`
13+
Type string `json:"type"`
1314

1415
// User is the user that authenticated with the provider.
1516
User *GitAuthUser `json:"user"`
17+
// AppInstallable is true if the request for app installs was successful.
18+
AppInstallable bool `json:"app_installable"`
1619
// AppInstallations are the installations that the user has access to.
1720
AppInstallations []GitAuthAppInstallation `json:"installations"`
21+
// AppInstallURL is the URL to install the app.
22+
AppInstallURL string `json:"app_install_url"`
1823
}
1924

2025
type GitAuthAppInstallation struct {
@@ -24,10 +29,10 @@ type GitAuthAppInstallation struct {
2429
}
2530

2631
type GitAuthUser struct {
27-
Login string
28-
AvatarURL string
29-
ProfileURL string
30-
Name string
32+
Login string `json:"login"`
33+
AvatarURL string `json:"avatar_url"`
34+
ProfileURL string `json:"profile_url"`
35+
Name string `json:"name"`
3136
}
3237

3338
// GitAuthDevice is the response from the device authorization endpoint.

docs/api/general.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ curl -X GET http://coder-server:8080/api/v2/deployment/config \
200200
"git_auth": {
201201
"value": [
202202
{
203+
"app_install_url": "string",
204+
"app_installations_url": "string",
203205
"auth_url": "string",
204206
"client_id": "string",
205207
"device_auth_url": "string",

docs/api/schemas.md

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@
593593
{
594594
"value": [
595595
{
596+
"app_install_url": "string",
597+
"app_installations_url": "string",
596598
"auth_url": "string",
597599
"client_id": "string",
598600
"device_auth_url": "string",
@@ -1879,6 +1881,8 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
18791881
"git_auth": {
18801882
"value": [
18811883
{
1884+
"app_install_url": "string",
1885+
"app_installations_url": "string",
18821886
"auth_url": "string",
18831887
"client_id": "string",
18841888
"device_auth_url": "string",
@@ -2212,6 +2216,8 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
22122216
"git_auth": {
22132217
"value": [
22142218
{
2219+
"app_install_url": "string",
2220+
"app_installations_url": "string",
22152221
"auth_url": "string",
22162222
"client_id": "string",
22172223
"device_auth_url": "string",
@@ -2581,6 +2587,8 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
25812587

25822588
```json
25832589
{
2590+
"app_install_url": "string",
2591+
"app_installations_url": "string",
25842592
"auth_url": "string",
25852593
"client_id": "string",
25862594
"device_auth_url": "string",
@@ -2597,19 +2605,21 @@ AuthorizationObject can represent a "set" of objects, such as: all workspaces in
25972605

25982606
### Properties
25992607

2600-
| Name | Type | Required | Restrictions | Description |
2601-
| ----------------- | --------------- | -------- | ------------ | ----------- |
2602-
| `auth_url` | string | false | | |
2603-
| `client_id` | string | false | | |
2604-
| `device_auth_url` | string | false | | |
2605-
| `device_flow` | boolean | false | | |
2606-
| `id` | string | false | | |
2607-
| `no_refresh` | boolean | false | | |
2608-
| `regex` | string | false | | |
2609-
| `scopes` | array of string | false | | |
2610-
| `token_url` | string | false | | |
2611-
| `type` | string | false | | |
2612-
| `validate_url` | string | false | | |
2608+
| Name | Type | Required | Restrictions | Description |
2609+
| ----------------------- | --------------- | -------- | ------------ | ----------- |
2610+
| `app_install_url` | string | false | | |
2611+
| `app_installations_url` | string | false | | |
2612+
| `auth_url` | string | false | | |
2613+
| `client_id` | string | false | | |
2614+
| `device_auth_url` | string | false | | |
2615+
| `device_flow` | boolean | false | | |
2616+
| `id` | string | false | | |
2617+
| `no_refresh` | boolean | false | | |
2618+
| `regex` | string | false | | |
2619+
| `scopes` | array of string | false | | |
2620+
| `token_url` | string | false | | |
2621+
| `type` | string | false | | |
2622+
| `validate_url` | string | false | | |
26132623

26142624
## codersdk.GitProvider
26152625

site/src/AppRouter.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,7 @@ export const AppRouter: FC = () => {
196196
<Route element={<DashboardLayout />}>
197197
<Route index element={<IndexPage />} />
198198

199-
<Route path="gitauth">
200-
<Route index element={<GitAuthPage />} />
201-
<Route path=":provider">
202-
<Route path="device" element={<GitAuthDevicePage />} />
203-
</Route>
204-
</Route>
199+
<Route path="gitauth/:provider" element={<GitAuthPage />} />
205200

206201
<Route path="workspaces" element={<WorkspacesPage />} />
207202

site/src/api/api.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,25 @@ export const getExperiments = async (): Promise<TypesGen.Experiment[]> => {
799799
}
800800
}
801801

802-
export const exchangeGitAuth = async (
802+
export const getGitAuthProvider = async (
803803
provider: string,
804-
req: TypesGen.ExchangeGitAuthRequest,
804+
): Promise<TypesGen.GitAuth> => {
805+
const resp = await axios.get(`/api/v2/gitauth/${provider}`)
806+
return resp.data
807+
}
808+
809+
export const getGitAuthDevice = async (
810+
provider: string,
811+
): Promise<TypesGen.GitAuthDevice> => {
812+
const resp = await axios.get(`/api/v2/gitauth/${provider}/device`)
813+
return resp.data
814+
}
815+
816+
export const exchangeGitAuthDevice = async (
817+
provider: string,
818+
req: TypesGen.GitAuthDeviceExchange,
805819
): Promise<void> => {
806-
const resp = await axios.post(`/gitauth/${provider}/exchange`, req)
820+
const resp = await axios.post(`/api/v2/gitauth/${provider}/device`, req)
807821
return resp.data
808822
}
809823

0 commit comments

Comments
 (0)