2016-08-14 07:11:52 +08:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2016-12-21 20:13:17 +08:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2016-08-14 07:11:52 +08:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-10-31 05:34:25 +08:00
|
|
|
"errors"
|
2016-08-14 07:11:52 +08:00
|
|
|
"fmt"
|
2018-09-12 22:46:02 +08:00
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
2016-08-14 07:11:52 +08:00
|
|
|
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2018-09-12 22:46:02 +08:00
|
|
|
"code.gitea.io/gitea/modules/auth/oauth2"
|
2018-10-31 05:34:25 +08:00
|
|
|
"code.gitea.io/gitea/modules/generate"
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-12-31 22:45:46 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-04-12 15:44:54 +08:00
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2016-08-14 07:11:52 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-11-04 19:42:18 +08:00
|
|
|
// CmdAdmin represents the available admin sub-command.
|
2016-08-14 07:11:52 +08:00
|
|
|
CmdAdmin = cli.Command{
|
|
|
|
Name: "admin",
|
2018-01-13 06:16:49 +08:00
|
|
|
Usage: "Command line interface to perform common administrative operations",
|
2016-08-14 07:11:52 +08:00
|
|
|
Subcommands: []cli.Command{
|
|
|
|
subcmdCreateUser,
|
2017-03-20 16:23:38 +08:00
|
|
|
subcmdChangePassword,
|
2017-12-31 22:45:46 +08:00
|
|
|
subcmdRepoSyncReleases,
|
2018-05-17 09:35:07 +08:00
|
|
|
subcmdRegenerate,
|
2018-09-12 22:46:02 +08:00
|
|
|
subcmdAuth,
|
2016-08-14 07:11:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
subcmdCreateUser = cli.Command{
|
|
|
|
Name: "create-user",
|
|
|
|
Usage: "Create a new user in database",
|
|
|
|
Action: runCreateUser,
|
|
|
|
Flags: []cli.Flag{
|
2016-11-10 06:18:22 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Usage: "Username",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
2016-11-10 06:32:24 +08:00
|
|
|
Usage: "User password",
|
2016-11-10 06:18:22 +08:00
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "email",
|
2016-11-10 06:32:24 +08:00
|
|
|
Usage: "User email address",
|
2016-11-10 06:18:22 +08:00
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "admin",
|
|
|
|
Usage: "User is an admin",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
2018-10-31 05:34:25 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "random-password",
|
|
|
|
Usage: "Generate a random password for the user",
|
|
|
|
},
|
2018-10-21 06:05:01 +08:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "must-change-password",
|
|
|
|
Usage: "Force the user to change his/her password after initial login",
|
|
|
|
},
|
2018-10-31 05:34:25 +08:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "random-password-length",
|
|
|
|
Usage: "Length of the random password to be generated",
|
|
|
|
Value: 12,
|
|
|
|
},
|
2016-08-14 07:11:52 +08:00
|
|
|
},
|
|
|
|
}
|
2017-03-20 16:23:38 +08:00
|
|
|
|
|
|
|
subcmdChangePassword = cli.Command{
|
|
|
|
Name: "change-password",
|
|
|
|
Usage: "Change a user's password",
|
|
|
|
Action: runChangePassword,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username,u",
|
|
|
|
Value: "",
|
|
|
|
Usage: "The user to change password for",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password,p",
|
|
|
|
Value: "",
|
|
|
|
Usage: "New password to set for user",
|
|
|
|
},
|
2018-06-10 06:35:14 +08:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
2017-03-20 16:23:38 +08:00
|
|
|
},
|
|
|
|
}
|
2017-12-31 22:45:46 +08:00
|
|
|
|
|
|
|
subcmdRepoSyncReleases = cli.Command{
|
|
|
|
Name: "repo-sync-releases",
|
|
|
|
Usage: "Synchronize repository releases with tags",
|
|
|
|
Action: runRepoSyncReleases,
|
|
|
|
}
|
2018-05-17 09:35:07 +08:00
|
|
|
|
|
|
|
subcmdRegenerate = cli.Command{
|
|
|
|
Name: "regenerate",
|
|
|
|
Usage: "Regenerate specific files",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
microcmdRegenHooks,
|
|
|
|
microcmdRegenKeys,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdRegenHooks = cli.Command{
|
|
|
|
Name: "hooks",
|
|
|
|
Usage: "Regenerate git-hooks",
|
|
|
|
Action: runRegenerateHooks,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdRegenKeys = cli.Command{
|
|
|
|
Name: "keys",
|
|
|
|
Usage: "Regenerate authorized_keys file",
|
|
|
|
Action: runRegenerateKeys,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-09-12 22:46:02 +08:00
|
|
|
|
|
|
|
subcmdAuth = cli.Command{
|
|
|
|
Name: "auth",
|
|
|
|
Usage: "Modify external auth providers",
|
|
|
|
Subcommands: []cli.Command{
|
|
|
|
microcmdAuthAddOauth,
|
|
|
|
microcmdAuthUpdateOauth,
|
|
|
|
microcmdAuthList,
|
|
|
|
microcmdAuthDelete,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdAuthList = cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List auth sources",
|
|
|
|
Action: runListAuth,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
idFlag = cli.Int64Flag{
|
|
|
|
Name: "id",
|
|
|
|
Usage: "ID of OAuth authentication source",
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdAuthDelete = cli.Command{
|
|
|
|
Name: "delete",
|
|
|
|
Usage: "Delete specific auth source",
|
|
|
|
Action: runDeleteAuth,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
idFlag,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
oauthCLIFlags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "config, c",
|
|
|
|
Value: "custom/conf/app.ini",
|
|
|
|
Usage: "Custom configuration file path",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "name",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Application Name",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "provider",
|
|
|
|
Value: "",
|
|
|
|
Usage: "OAuth2 Provider",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "key",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Client ID (Key)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "secret",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Client Secret",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "auto-discover-url",
|
|
|
|
Value: "",
|
|
|
|
Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "use-custom-urls",
|
|
|
|
Value: "false",
|
|
|
|
Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "custom-auth-url",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "custom-token-url",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Use a custom Token URL (option for GitLab/GitHub)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "custom-profile-url",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "custom-email-url",
|
|
|
|
Value: "",
|
|
|
|
Usage: "Use a custom Email URL (option for GitHub)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdAuthUpdateOauth = cli.Command{
|
|
|
|
Name: "update-oauth",
|
|
|
|
Usage: "Update existing Oauth authentication source",
|
|
|
|
Action: runUpdateOauth,
|
|
|
|
Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
|
|
|
|
}
|
|
|
|
|
|
|
|
microcmdAuthAddOauth = cli.Command{
|
|
|
|
Name: "add-oauth",
|
|
|
|
Usage: "Add new Oauth authentication source",
|
|
|
|
Action: runAddOauth,
|
|
|
|
Flags: oauthCLIFlags,
|
|
|
|
}
|
2016-08-14 07:11:52 +08:00
|
|
|
)
|
|
|
|
|
2017-03-20 16:23:38 +08:00
|
|
|
func runChangePassword(c *cli.Context) error {
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := argsSet(c, "username", "password"); err != nil {
|
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
2018-06-10 06:35:14 +08:00
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uname := c.String("username")
|
|
|
|
user, err := models.GetUserByName(uname)
|
|
|
|
if err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
if user.Salt, err = models.GetUserSalt(); err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
2018-01-12 06:19:38 +08:00
|
|
|
user.HashPassword(c.String("password"))
|
2017-08-12 22:18:44 +08:00
|
|
|
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2017-03-20 16:23:38 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
fmt.Printf("%s's password has been successfully updated!\n", user.Name)
|
2017-03-20 16:23:38 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-14 07:11:52 +08:00
|
|
|
func runCreateUser(c *cli.Context) error {
|
2018-10-31 05:34:25 +08:00
|
|
|
if err := argsSet(c, "name", "email"); err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return err
|
2016-08-14 07:11:52 +08:00
|
|
|
}
|
|
|
|
|
2018-10-31 05:34:25 +08:00
|
|
|
if c.IsSet("password") && c.IsSet("random-password") {
|
|
|
|
return errors.New("cannot set both -random-password and -password flags")
|
|
|
|
}
|
|
|
|
|
|
|
|
var password string
|
|
|
|
|
|
|
|
if c.IsSet("password") {
|
|
|
|
password = c.String("password")
|
|
|
|
} else if c.IsSet("random-password") {
|
2018-11-26 23:00:38 +08:00
|
|
|
var err error
|
|
|
|
password, err = generate.GetRandomString(c.Int("random-password-length"))
|
2018-10-31 05:34:25 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("generated random password is '%s'\n", password)
|
|
|
|
} else {
|
|
|
|
return errors.New("must set either password or random-password flag")
|
|
|
|
}
|
|
|
|
|
2016-08-14 07:11:52 +08:00
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-01-05 16:31:07 +08:00
|
|
|
}
|
2016-08-14 07:11:52 +08:00
|
|
|
|
2018-10-21 06:05:01 +08:00
|
|
|
// always default to true
|
|
|
|
var changePassword = true
|
|
|
|
|
2018-11-25 15:42:24 +08:00
|
|
|
// If this is the first user being created.
|
|
|
|
// Take it as the admin and don't force a password update.
|
|
|
|
if n := models.CountUsers(); n == 0 {
|
|
|
|
changePassword = false
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:05:01 +08:00
|
|
|
if c.IsSet("must-change-password") {
|
|
|
|
changePassword = c.Bool("must-change-password")
|
|
|
|
}
|
|
|
|
|
2016-08-14 07:11:52 +08:00
|
|
|
if err := models.CreateUser(&models.User{
|
2018-10-21 06:05:01 +08:00
|
|
|
Name: c.String("name"),
|
|
|
|
Email: c.String("email"),
|
2018-10-31 05:34:25 +08:00
|
|
|
Passwd: password,
|
2018-10-21 06:05:01 +08:00
|
|
|
IsActive: true,
|
|
|
|
IsAdmin: c.Bool("admin"),
|
|
|
|
MustChangePassword: changePassword,
|
2019-01-10 01:22:57 +08:00
|
|
|
Theme: setting.UI.DefaultTheme,
|
2016-08-14 07:11:52 +08:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("CreateUser: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-31 22:45:46 +08:00
|
|
|
|
|
|
|
func runRepoSyncReleases(c *cli.Context) error {
|
2018-01-13 06:16:49 +08:00
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
2017-12-31 22:45:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Synchronizing repository releases (this may take a while)")
|
|
|
|
for page := 1; ; page++ {
|
|
|
|
repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
|
|
|
|
Page: page,
|
|
|
|
PageSize: models.RepositoryListDefaultPageSize,
|
|
|
|
Private: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-13 06:16:49 +08:00
|
|
|
return fmt.Errorf("SearchRepositoryByName: %v", err)
|
2017-12-31 22:45:46 +08:00
|
|
|
}
|
|
|
|
if len(repos) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
log.Trace("Processing next %d repos of %d", len(repos), count)
|
|
|
|
for _, repo := range repos {
|
|
|
|
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
|
|
|
|
gitRepo, err := git.OpenRepository(repo.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("OpenRepository: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
oldnum, err := getReleaseCount(repo.ID)
|
2017-12-31 22:45:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
}
|
|
|
|
log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
|
|
|
|
|
|
|
|
if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
|
|
|
|
log.Warn(" SyncReleasesWithTags: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-13 06:16:49 +08:00
|
|
|
count, err = getReleaseCount(repo.ID)
|
2017-12-31 22:45:46 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn(" GetReleaseCountByRepoID: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace(" repo %s releases synchronized to tags: from %d to %d",
|
|
|
|
repo.FullName(), oldnum, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-13 06:16:49 +08:00
|
|
|
|
|
|
|
func getReleaseCount(id int64) (int64, error) {
|
|
|
|
return models.GetReleaseCountByRepoID(
|
|
|
|
id,
|
|
|
|
models.FindReleasesOptions{
|
|
|
|
IncludeTags: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2018-05-17 09:35:07 +08:00
|
|
|
|
|
|
|
func runRegenerateHooks(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return models.SyncRepositoryHooks()
|
|
|
|
}
|
|
|
|
|
|
|
|
func runRegenerateKeys(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return models.RewriteAllPublicKeys()
|
|
|
|
}
|
2018-09-12 22:46:02 +08:00
|
|
|
|
|
|
|
func parseOAuth2Config(c *cli.Context) *models.OAuth2Config {
|
|
|
|
var customURLMapping *oauth2.CustomURLMapping
|
|
|
|
if c.IsSet("use-custom-urls") {
|
|
|
|
customURLMapping = &oauth2.CustomURLMapping{
|
|
|
|
TokenURL: c.String("custom-token-url"),
|
|
|
|
AuthURL: c.String("custom-auth-url"),
|
|
|
|
ProfileURL: c.String("custom-profile-url"),
|
|
|
|
EmailURL: c.String("custom-email-url"),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
customURLMapping = nil
|
|
|
|
}
|
|
|
|
return &models.OAuth2Config{
|
|
|
|
Provider: c.String("provider"),
|
|
|
|
ClientID: c.String("key"),
|
|
|
|
ClientSecret: c.String("secret"),
|
|
|
|
OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
|
|
|
|
CustomURLMapping: customURLMapping,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func runAddOauth(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:51:07 +08:00
|
|
|
return models.CreateLoginSource(&models.LoginSource{
|
2018-09-12 22:46:02 +08:00
|
|
|
Type: models.LoginOAuth2,
|
|
|
|
Name: c.String("name"),
|
|
|
|
IsActived: true,
|
|
|
|
Cfg: parseOAuth2Config(c),
|
2018-10-18 12:51:07 +08:00
|
|
|
})
|
2018-09-12 22:46:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runUpdateOauth(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.IsSet("id") {
|
|
|
|
return fmt.Errorf("--id flag is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := models.GetLoginSourceByID(c.Int64("id"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oAuth2Config := source.OAuth2()
|
|
|
|
|
|
|
|
if c.IsSet("name") {
|
|
|
|
source.Name = c.String("name")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("provider") {
|
|
|
|
oAuth2Config.Provider = c.String("provider")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("key") {
|
|
|
|
oAuth2Config.ClientID = c.String("key")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("secret") {
|
|
|
|
oAuth2Config.ClientSecret = c.String("secret")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("auto-discover-url") {
|
|
|
|
oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
|
|
|
|
}
|
|
|
|
|
|
|
|
// update custom URL mapping
|
|
|
|
var customURLMapping *oauth2.CustomURLMapping
|
|
|
|
|
|
|
|
if oAuth2Config.CustomURLMapping != nil {
|
|
|
|
customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
|
|
|
|
customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
|
|
|
|
customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
|
|
|
|
customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
|
|
|
|
}
|
|
|
|
if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
|
|
|
|
customURLMapping.TokenURL = c.String("custom-token-url")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
|
|
|
|
customURLMapping.AuthURL = c.String("custom-auth-url")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
|
|
|
|
customURLMapping.ProfileURL = c.String("custom-profile-url")
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
|
|
|
|
customURLMapping.EmailURL = c.String("custom-email-url")
|
|
|
|
}
|
|
|
|
|
|
|
|
oAuth2Config.CustomURLMapping = customURLMapping
|
|
|
|
source.Cfg = oAuth2Config
|
|
|
|
|
2018-10-18 12:51:07 +08:00
|
|
|
return models.UpdateSource(source)
|
2018-09-12 22:46:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func runListAuth(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
loginSources, err := models.LoginSources()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop through each source and print
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.AlignRight)
|
|
|
|
fmt.Fprintf(w, "ID\tName\tType\tEnabled")
|
|
|
|
for _, source := range loginSources {
|
|
|
|
fmt.Fprintf(w, "%d\t%s\t%s\t%t", source.ID, source.Name, models.LoginNames[source.Type], source.IsActived)
|
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func runDeleteAuth(c *cli.Context) error {
|
|
|
|
if c.IsSet("config") {
|
|
|
|
setting.CustomConf = c.String("config")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.IsSet("id") {
|
|
|
|
return fmt.Errorf("--id flag is missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := models.GetLoginSourceByID(c.Int64("id"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-18 12:51:07 +08:00
|
|
|
return models.DeleteSource(source)
|
2018-09-12 22:46:02 +08:00
|
|
|
}
|