6
6
package user
7
7
8
8
import (
9
+ "crypto/rand"
10
+ "encoding/hex"
9
11
"errors"
10
12
"fmt"
11
13
"net/http"
@@ -116,6 +118,7 @@ func SignIn(ctx *context.Context) {
116
118
return
117
119
}
118
120
121
+ ctx .Data ["AllowPassword" ] = true
119
122
orderedOAuth2Names , oauth2Providers , err := models .GetActiveOAuth2Providers ()
120
123
if err != nil {
121
124
ctx .ServerError ("UserSignIn" , err )
@@ -135,6 +138,7 @@ func SignIn(ctx *context.Context) {
135
138
func SignInPost (ctx * context.Context , form auth.SignInForm ) {
136
139
ctx .Data ["Title" ] = ctx .Tr ("sign_in" )
137
140
141
+ ctx .Data ["AllowPassword" ] = true
138
142
orderedOAuth2Names , oauth2Providers , err := models .GetActiveOAuth2Providers ()
139
143
if err != nil {
140
144
ctx .ServerError ("UserSignIn" , err )
@@ -658,9 +662,10 @@ func oAuth2UserLoginCallback(loginSource *models.LoginSource, request *http.Requ
658
662
659
663
// LinkAccount shows the page where the user can decide to login or create a new account
660
664
func LinkAccount (ctx * context.Context ) {
665
+ ctx .Data ["AllowPassword" ] = setting .Service .RequireExternalRegistrationPassword && ! setting .Service .AllowOnlyExternalRegistration
661
666
ctx .Data ["Title" ] = ctx .Tr ("link_account" )
662
667
ctx .Data ["LinkAccountMode" ] = true
663
- ctx .Data ["EnableCaptcha" ] = setting .Service .EnableCaptcha
668
+ ctx .Data ["EnableCaptcha" ] = setting .Service .RequireExternalRegistrationCaptcha
664
669
ctx .Data ["CaptchaType" ] = setting .Service .CaptchaType
665
670
ctx .Data ["RecaptchaURL" ] = setting .Service .RecaptchaURL
666
671
ctx .Data ["RecaptchaSitekey" ] = setting .Service .RecaptchaSitekey
@@ -707,10 +712,11 @@ func LinkAccount(ctx *context.Context) {
707
712
708
713
// LinkAccountPostSignIn handle the coupling of external account with another account using signIn
709
714
func LinkAccountPostSignIn (ctx * context.Context , signInForm auth.SignInForm ) {
715
+ ctx .Data ["AllowPassword" ] = ! setting .Service .AllowOnlyExternalRegistration
710
716
ctx .Data ["Title" ] = ctx .Tr ("link_account" )
711
717
ctx .Data ["LinkAccountMode" ] = true
712
718
ctx .Data ["LinkAccountModeSignIn" ] = true
713
- ctx .Data ["EnableCaptcha" ] = setting .Service .EnableCaptcha
719
+ ctx .Data ["EnableCaptcha" ] = setting .Service .RequireExternalRegistrationCaptcha
714
720
ctx .Data ["RecaptchaURL" ] = setting .Service .RecaptchaURL
715
721
ctx .Data ["CaptchaType" ] = setting .Service .CaptchaType
716
722
ctx .Data ["RecaptchaSitekey" ] = setting .Service .RecaptchaSitekey
@@ -776,10 +782,13 @@ func LinkAccountPostSignIn(ctx *context.Context, signInForm auth.SignInForm) {
776
782
777
783
// LinkAccountPostRegister handle the creation of a new account for an external account using signUp
778
784
func LinkAccountPostRegister (ctx * context.Context , cpt * captcha.Captcha , form auth.RegisterForm ) {
785
+ // TODO Make insecure passwords optional for local accounts also,
786
+ // once email-based Second-Factor Auth is available
787
+ ctx .Data ["AllowPassword" ] = setting .Service .RequireExternalRegistrationPassword && ! setting .Service .AllowOnlyExternalRegistration
779
788
ctx .Data ["Title" ] = ctx .Tr ("link_account" )
780
789
ctx .Data ["LinkAccountMode" ] = true
781
790
ctx .Data ["LinkAccountModeRegister" ] = true
782
- ctx .Data ["EnableCaptcha" ] = setting .Service .EnableCaptcha
791
+ ctx .Data ["EnableCaptcha" ] = setting .Service .RequireExternalRegistrationCaptcha
783
792
ctx .Data ["RecaptchaURL" ] = setting .Service .RecaptchaURL
784
793
ctx .Data ["CaptchaType" ] = setting .Service .CaptchaType
785
794
ctx .Data ["RecaptchaSitekey" ] = setting .Service .RecaptchaSitekey
@@ -821,15 +830,30 @@ func LinkAccountPostRegister(ctx *context.Context, cpt *captcha.Captcha, form au
821
830
}
822
831
}
823
832
824
- if (len (strings .TrimSpace (form .Password )) > 0 || len (strings .TrimSpace (form .Retype )) > 0 ) && form .Password != form .Retype {
825
- ctx .Data ["Err_Password" ] = true
826
- ctx .RenderWithErr (ctx .Tr ("form.password_not_match" ), tplLinkAccount , & form )
827
- return
828
- }
829
- if len (strings .TrimSpace (form .Password )) > 0 && len (form .Password ) < setting .MinPasswordLength {
830
- ctx .Data ["Err_Password" ] = true
831
- ctx .RenderWithErr (ctx .Tr ("auth.password_too_short" , setting .MinPasswordLength ), tplLinkAccount , & form )
832
- return
833
+ if setting .Service .AllowOnlyExternalRegistration || ! setting .Service .RequireExternalRegistrationPassword {
834
+ // Generating a random password a stop-gap shim to get around the password requirement.
835
+ // Eventually the database should be changed to indicate "Second Factor"-enabled accounts
836
+ // (accounts that do not introduce the security vulnerabilities of a password).
837
+ // If a user decides to circumvent second-factor security, and purposefully create a password,
838
+ // they can still do so using the "Recover Account" option.
839
+ bytes := make ([]byte , 16 )
840
+ _ , err := rand .Read (bytes )
841
+ if nil != err {
842
+ ctx .ServerError ("CreateUser" , err )
843
+ return
844
+ }
845
+ form .Password = hex .EncodeToString (bytes )
846
+ } else {
847
+ if (len (strings .TrimSpace (form .Password )) > 0 || len (strings .TrimSpace (form .Retype )) > 0 ) && form .Password != form .Retype {
848
+ ctx .Data ["Err_Password" ] = true
849
+ ctx .RenderWithErr (ctx .Tr ("form.password_not_match" ), tplLinkAccount , & form )
850
+ return
851
+ }
852
+ if len (strings .TrimSpace (form .Password )) > 0 && len (form .Password ) < setting .MinPasswordLength {
853
+ ctx .Data ["Err_Password" ] = true
854
+ ctx .RenderWithErr (ctx .Tr ("auth.password_too_short" , setting .MinPasswordLength ), tplLinkAccount , & form )
855
+ return
856
+ }
833
857
}
834
858
835
859
loginSource , err := models .GetActiveOAuth2LoginSourceByName (gothUser .(goth.User ).Provider )
@@ -916,6 +940,8 @@ func SignOut(ctx *context.Context) {
916
940
917
941
// SignUp render the register page
918
942
func SignUp (ctx * context.Context ) {
943
+ ctx .Data ["AllowPassword" ] = true
944
+
919
945
ctx .Data ["Title" ] = ctx .Tr ("sign_up" )
920
946
921
947
ctx .Data ["SignUpLink" ] = setting .AppSubURL + "/user/sign_up"
@@ -932,6 +958,8 @@ func SignUp(ctx *context.Context) {
932
958
933
959
// SignUpPost response for sign up information submission
934
960
func SignUpPost (ctx * context.Context , cpt * captcha.Captcha , form auth.RegisterForm ) {
961
+ ctx .Data ["AllowPassword" ] = true
962
+
935
963
ctx .Data ["Title" ] = ctx .Tr ("sign_up" )
936
964
937
965
ctx .Data ["SignUpLink" ] = setting .AppSubURL + "/user/sign_up"
0 commit comments