Skip to content

Commit 3532582

Browse files
committed
feat(go): improve error message.
1 parent 6ccfd10 commit 3532582

File tree

5 files changed

+63
-6
lines changed

5 files changed

+63
-6
lines changed

pro/auth/azure-ad.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,18 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
7979
handleOauthUserSignUpApprovalPending(w)
8080
return
8181
}
82-
// if user exists with provider ID, convert them into email ID
82+
8383
user, err := logic.GetUser(content.UserPrincipalName)
8484
if err == nil {
85+
// if user exists, then ensure user's auth type is
86+
// oauth before proceeding.
87+
if user.AuthType == models.BasicAuth {
88+
logger.Log(0, "invalid auth type: basic_auth")
89+
handleAuthTypeMismatch(w)
90+
return
91+
}
92+
93+
// if user exists with provider ID, convert them into email ID
8594
_, err := logic.GetUser(content.Email)
8695
if err != nil {
8796
user.UserName = content.Email
@@ -91,7 +100,8 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
91100
database.Insert(user.UserName, string(d), database.USERS_TABLE_NAME)
92101
}
93102
}
94-
_, err = logic.GetUser(content.Email)
103+
104+
user, err = logic.GetUser(content.Email)
95105
if err != nil {
96106
if database.IsEmptyRecord(err) { // user must not exist, so try to make one
97107
if inviteExists {
@@ -127,7 +137,16 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
127137
handleSomethingWentWrong(w)
128138
return
129139
}
140+
} else {
141+
// if user exists, then ensure user's auth type is
142+
// oauth before proceeding.
143+
if user.AuthType == models.BasicAuth {
144+
logger.Log(0, "invalid auth type: basic_auth")
145+
handleAuthTypeMismatch(w)
146+
return
147+
}
130148
}
149+
131150
user, err = logic.GetUser(content.Email)
132151
if err != nil {
133152
handleOauthUserNotFound(w)

pro/auth/error.go

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ var somethingwentwrong = fmt.Sprintf(htmlBaseTemplate, `<h2>Something went wrong
110110

111111
var notallowedtosignup = fmt.Sprintf(htmlBaseTemplate, `<h2>Your email is not allowed. Please contact your administrator.</h2>`)
112112

113+
var authTypeMismatch = fmt.Sprintf(htmlBaseTemplate, `<h2>It looks like you already have an account with us using Basic Authentication.</h2>
114+
<p>To continue, please log in with your existing credentials or reset your password if needed.</p>`)
115+
113116
func handleOauthUserNotFound(response http.ResponseWriter) {
114117
response.Header().Set("Content-Type", "text/html; charset=utf-8")
115118
response.WriteHeader(http.StatusNotFound)
@@ -157,3 +160,9 @@ func handleSomethingWentWrong(response http.ResponseWriter) {
157160
response.WriteHeader(http.StatusInternalServerError)
158161
response.Write([]byte(somethingwentwrong))
159162
}
163+
164+
func handleAuthTypeMismatch(response http.ResponseWriter) {
165+
response.Header().Set("Content-Type", "text/html; charset=utf-8")
166+
response.WriteHeader(http.StatusBadRequest)
167+
response.Write([]byte(authTypeMismatch))
168+
}

pro/auth/github.go

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
8282
// if user exists with provider ID, convert them into email ID
8383
user, err := logic.GetUser(content.Login)
8484
if err == nil {
85+
// if user exists, then ensure user's auth type is
86+
// oauth before proceeding.
87+
if user.AuthType == models.BasicAuth {
88+
logger.Log(0, "invalid auth type: basic_auth")
89+
handleAuthTypeMismatch(w)
90+
return
91+
}
92+
8593
// checks if user exists with email
8694
_, err := logic.GetUser(content.Email)
8795
if err != nil {

pro/auth/google.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
8080
handleOauthUserSignUpApprovalPending(w)
8181
return
8282
}
83-
_, err = logic.GetUser(content.Email)
83+
84+
user, err := logic.GetUser(content.Email)
8485
if err != nil {
8586
if database.IsEmptyRecord(err) { // user must not exist, so try to make one
8687
if inviteExists {
@@ -117,13 +118,23 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
117118
handleSomethingWentWrong(w)
118119
return
119120
}
121+
} else {
122+
// if user exists, then ensure user's auth type is
123+
// oauth before proceeding.
124+
if user.AuthType == models.BasicAuth {
125+
logger.Log(0, "invalid auth type: basic_auth")
126+
handleAuthTypeMismatch(w)
127+
return
128+
}
120129
}
121-
user, err := logic.GetUser(content.Email)
130+
131+
user, err = logic.GetUser(content.Email)
122132
if err != nil {
123133
logger.Log(0, "error fetching user: ", err.Error())
124134
handleOauthUserNotFound(w)
125135
return
126136
}
137+
127138
userRole, err := logic.GetRole(user.PlatformRoleID)
128139
if err != nil {
129140
handleSomethingWentWrong(w)

pro/auth/oidc.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
9191
handleOauthUserSignUpApprovalPending(w)
9292
return
9393
}
94-
_, err = logic.GetUser(content.Email)
94+
95+
user, err := logic.GetUser(content.Email)
9596
if err != nil {
9697
if database.IsEmptyRecord(err) { // user must not exist, so try to make one
9798
if inviteExists {
@@ -127,8 +128,17 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
127128
handleSomethingWentWrong(w)
128129
return
129130
}
131+
} else {
132+
// if user exists, then ensure user's auth type is
133+
// oauth before proceeding.
134+
if user.AuthType == models.BasicAuth {
135+
logger.Log(0, "invalid auth type: basic_auth")
136+
handleAuthTypeMismatch(w)
137+
return
138+
}
130139
}
131-
user, err := logic.GetUser(content.Email)
140+
141+
user, err = logic.GetUser(content.Email)
132142
if err != nil {
133143
handleOauthUserNotFound(w)
134144
return

0 commit comments

Comments
 (0)