func SignInOAuthCallback(ctx *context.Context) {
	provider := ctx.Params(":provider")

	// first look if the provider is still active
	loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
	if err != nil {
		ctx.ServerError("SignIn", err)
		return
	}

	if loginSource == nil {
		ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
		return
	}

	u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req, ctx.Resp)

	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}

	if u == nil {
		if !(setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration) && setting.OAuth2Client.EnableAutoRegistration {
			// create new user with details from oauth2 provider
			var missingFields []string
			if gothUser.UserID == "" {
				missingFields = append(missingFields, "sub")
			}
			if gothUser.Email == "" {
				missingFields = append(missingFields, "email")
			}
			if setting.OAuth2Client.Username == setting.OAuth2UsernameNickname && gothUser.NickName == "" {
				missingFields = append(missingFields, "nickname")
			}
			if len(missingFields) > 0 {
				log.Error("OAuth2 Provider %s returned empty or missing fields: %s", loginSource.Name, missingFields)
				if loginSource.IsOAuth2() && loginSource.OAuth2().Provider == "openidConnect" {
					log.Error("You may need to change the 'OPENID_CONNECT_SCOPES' setting to request all required fields")
				}
				err = fmt.Errorf("OAuth2 Provider %s returned empty or missing fields: %s", loginSource.Name, missingFields)
				ctx.ServerError("CreateUser", err)
				return
			}
			u = &models.User{
				Name:        getUserName(&gothUser),
				FullName:    gothUser.Name,
				Email:       gothUser.Email,
				IsActive:    !setting.OAuth2Client.RegisterEmailConfirm,
				LoginType:   models.LoginOAuth2,
				LoginSource: loginSource.ID,
				LoginName:   gothUser.UserID,
			}

			if !createAndHandleCreatedUser(ctx, base.TplName(""), nil, u, &gothUser, setting.OAuth2Client.AccountLinking != setting.OAuth2AccountLinkingDisabled) {
				// error already handled
				return
			}
		} else {
			// no existing user is found, request attach or new account
			showLinkingLogin(ctx, gothUser)
			return
		}
	}

	handleOAuth2SignIn(ctx, u, gothUser)
}
// SignInOAuthCallback handles the callback from the given provider
SignInOAuthCallback handles the callback from the given provider
// SignInOAuthCallback handles the callback from the given provider
func SignInOAuthCallback(ctx *context.Context) {
	provider := ctx.Params(":provider")
	loginSource, err := models.GetActiveOAuth2LoginSourceByName(provider)
	if err != nil {
		ctx.ServerError("SignIn", err)
		return
	}
	if loginSource == nil {
		ctx.ServerError("SignIn", errors.New("No valid provider found, check configured callback url in provider"))
		return
	}
	u, gothUser, err := oAuth2UserLoginCallback(loginSource, ctx.Req, ctx.Resp)
	if err != nil {
		ctx.ServerError("UserSignIn", err)
		return
	}
	if u == nil {
		if !(setting.Service.DisableRegistration || setting.Service.AllowOnlyInternalRegistration) && setting.OAuth2Client.EnableAutoRegistration {
			var missingFields []string
			if gothUser.UserID == "" {
				missingFields = append(missingFields, "sub")
			}
			if gothUser.Email == "" {
				missingFields = append(missingFields, "email")
			}
			if setting.OAuth2Client.Username == setting.OAuth2UsernameNickname && gothUser.NickName == "" {
				missingFields = append(missingFields, "nickname")
			}
			if len(missingFields) > 0 {
				log.Error("OAuth2 Provider %s returned empty or missing fields: %s", loginSource.Name, missingFields)
				if loginSource.IsOAuth2() && loginSource.OAuth2().Provider == "openidConnect" {
					log.Error("You may need to change the 'OPENID_CONNECT_SCOPES' setting to request all required fields")
				}
				err = fmt.Errorf("OAuth2 Provider %s returned empty or missing fields: %s", loginSource.Name, missingFields)
				ctx.ServerError("CreateUser", err)
				return
			}
			u = &models.User{
				Name:        getUserName(&gothUser),
				FullName:    gothUser.Name,
				Email:       gothUser.Email,
				IsActive:    !setting.OAuth2Client.RegisterEmailConfirm,
				LoginType:   models.LoginOAuth2,
				LoginSource: loginSource.ID,
				LoginName:   gothUser.UserID,
			}
			if !createAndHandleCreatedUser(ctx, base.TplName(""), nil, u, &gothUser, setting.OAuth2Client.AccountLinking != setting.OAuth2AccountLinkingDisabled) {
				return
			}
		} else {
			showLinkingLogin(ctx, gothUser)
			return
		}
	}
	handleOAuth2SignIn(ctx, u, gothUser)
}
func SignInOAuthCallback ( ctx * context . Context ) { provider := ctx . Params ( " " ) loginSource , err := models . GetActiveOAuth2LoginSourceByName ( provider ) if err != nil { ctx . ServerError ( " " , err ) return } if loginSource == nil { ctx . ServerError ( " " , errors . New ( " " ) ) return } u , gothUser , err := oAuth2UserLoginCallback ( loginSource , ctx . Req , ctx . Resp ) if err != nil { ctx . ServerError ( " " , err ) return } if u == nil { if ! ( setting . Service . DisableRegistration || setting . Service . AllowOnlyInternalRegistration ) && setting . OAuth2Client . EnableAutoRegistration { var missingFields [ ] string if gothUser . UserID == " " { missingFields = append ( missingFields , " " ) } if gothUser . Email == " " { missingFields = append ( missingFields , " " ) } if setting . OAuth2Client . Username == setting . OAuth2UsernameNickname && gothUser . NickName == " " { missingFields = append ( missingFields , " " ) } if len ( missingFields ) > 0 { log . Error ( " " , loginSource . Name , missingFields ) if loginSource . IsOAuth2 ( ) && loginSource . OAuth2 ( ) . Provider == " " { log . Error ( " " ) } err = fmt . Errorf ( " " , loginSource . Name , missingFields ) ctx . ServerError ( " " , err ) return } u = & models . User { Name : getUserName ( & gothUser ) , FullName : gothUser . Name , Email : gothUser . Email , IsActive : ! setting . OAuth2Client . RegisterEmailConfirm , LoginType : models . LoginOAuth2 , LoginSource : loginSource . ID , LoginName : gothUser . UserID , } if ! createAndHandleCreatedUser ( ctx , base . TplName ( " " ) , nil , u , & gothUser , setting . OAuth2Client . AccountLinking != setting . OAuth2AccountLinkingDisabled ) { return } } else { showLinkingLogin ( ctx , gothUser ) return } } handleOAuth2SignIn ( ctx , u , gothUser ) }
SignInOAuthCallback handles the callback from the given provider
SignInOAuthCallback handles the callback from the given provider
// first look if the provider is still active // create new user with details from oauth2 provider // error already handled // no existing user is found, request attach or new account