Skip to content

Commit 68977f5

Browse files
committed
create a new route for user login type
1 parent 6294fb5 commit 68977f5

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

coderd/coderd.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,6 @@ func New(options *Options) *API {
593593
r.Get("/first", api.firstUser)
594594
r.Post("/first", api.postFirstUser)
595595
r.Route("/authmethods", func(r chi.Router) {
596-
// The API Key allows this method to return the auth method
597-
// for the logged-in user. This information is useful for the
598-
// caller. If not authenticated, this information is omitted.
599-
r.Use(apiKeyMiddlewareOptional)
600596
r.Get("/", api.userAuthMethods)
601597
})
602598

@@ -642,6 +638,7 @@ func New(options *Options) *API {
642638
r.Use(httpmw.ExtractUserParam(options.Database, false))
643639
r.Delete("/", api.deleteUser)
644640
r.Get("/", api.userByName)
641+
r.Get("/login-type", api.userLoginType)
645642
r.Put("/profile", api.putUserProfile)
646643
r.Route("/status", func(r chi.Router) {
647644
r.Put("/suspend", api.putSuspendUserAccount())

coderd/userauth.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,6 @@ type GithubOAuth2Config struct {
407407
// @Success 200 {object} codersdk.AuthMethods
408408
// @Router /users/authmethods [get]
409409
func (api *API) userAuthMethods(rw http.ResponseWriter, r *http.Request) {
410-
key, ok := httpmw.APIKeyOptional(r)
411-
var currentUserLoginType codersdk.LoginType
412-
if ok {
413-
user, err := api.Database.GetUserByID(r.Context(), key.UserID)
414-
if err != nil {
415-
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{
416-
Message: "Internal error.",
417-
Detail: err.Error(),
418-
})
419-
return
420-
}
421-
currentUserLoginType = codersdk.LoginType(user.LoginType)
422-
}
423410
var signInText string
424411
var iconURL string
425412

@@ -431,8 +418,7 @@ func (api *API) userAuthMethods(rw http.ResponseWriter, r *http.Request) {
431418
}
432419

433420
httpapi.Write(r.Context(), rw, http.StatusOK, codersdk.AuthMethods{
434-
UserAuthenticationType: currentUserLoginType,
435-
ConvertToOIDCEnabled: api.Options.DeploymentValues.EnableOauthAccountConversion.Value(),
421+
ConvertToOIDCEnabled: api.Options.DeploymentValues.EnableOauthAccountConversion.Value(),
436422
Password: codersdk.AuthMethod{
437423
Enabled: !api.DeploymentValues.DisablePasswordAuth.Value(),
438424
},

coderd/users.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,37 @@ func (api *API) userByName(rw http.ResponseWriter, r *http.Request) {
501501
httpapi.Write(ctx, rw, http.StatusOK, db2sdk.User(user, organizationIDs))
502502
}
503503

504+
// Returns the user's login type. This only works if the api key for authorization
505+
// and the requested user match. Eg: 'me'
506+
//
507+
// @Summary Get user login type
508+
// @ID get-user-login-type
509+
// @Security CoderSessionToken
510+
// @Produce json
511+
// @Tags Users
512+
// @Param user path string true "User ID, name, or me"
513+
// @Success 200 {object} codersdk.UserAuth
514+
// @Router /users/{user}/login-type [get]
515+
func (api *API) userLoginType(rw http.ResponseWriter, r *http.Request) {
516+
var (
517+
ctx = r.Context()
518+
user = httpmw.UserParam(r)
519+
key = httpmw.APIKey(r)
520+
)
521+
522+
if key.UserID != user.ID {
523+
// Currently this is only valid for querying yourself.
524+
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
525+
Message: "You are not authorized to view this user's login type.",
526+
})
527+
return
528+
}
529+
530+
httpapi.Write(ctx, rw, http.StatusOK, codersdk.UserAuth{
531+
LoginType: codersdk.LoginType(user.LoginType),
532+
})
533+
}
534+
504535
// @Summary Update user profile
505536
// @ID update-user-profile
506537
// @Security CoderSessionToken

codersdk/users.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,20 @@ type CreateOrganizationRequest struct {
123123

124124
// AuthMethods contains authentication method information like whether they are enabled or not or custom text, etc.
125125
type AuthMethods struct {
126-
// UserAuthenticationType returns the authentication method for the given
127-
// caller if the request is an authenticated request. Otherwise it is empty.
128-
UserAuthenticationType LoginType `json:"me_login_type,omitempty"`
129-
ConvertToOIDCEnabled bool `json:"convert_to_oidc_enabled"`
130-
Password AuthMethod `json:"password"`
131-
Github AuthMethod `json:"github"`
132-
OIDC OIDCAuthMethod `json:"oidc"`
126+
ConvertToOIDCEnabled bool `json:"convert_to_oidc_enabled"`
127+
Password AuthMethod `json:"password"`
128+
Github AuthMethod `json:"github"`
129+
OIDC OIDCAuthMethod `json:"oidc"`
133130
}
134131

135132
type AuthMethod struct {
136133
Enabled bool `json:"enabled"`
137134
}
138135

136+
type UserAuth struct {
137+
LoginType LoginType `json:"login_type"`
138+
}
139+
139140
type OIDCAuthMethod struct {
140141
AuthMethod
141142
SignInText string `json:"signInText"`

0 commit comments

Comments
 (0)