You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "context"
  8. "errors"
  9. "fmt"
  10. "os"
  11. "strings"
  12. "text/tabwriter"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/models/db"
  15. "code.gitea.io/gitea/models/login"
  16. user_model "code.gitea.io/gitea/models/user"
  17. "code.gitea.io/gitea/modules/git"
  18. "code.gitea.io/gitea/modules/graceful"
  19. "code.gitea.io/gitea/modules/log"
  20. pwd "code.gitea.io/gitea/modules/password"
  21. repo_module "code.gitea.io/gitea/modules/repository"
  22. "code.gitea.io/gitea/modules/setting"
  23. "code.gitea.io/gitea/modules/storage"
  24. auth_service "code.gitea.io/gitea/services/auth"
  25. "code.gitea.io/gitea/services/auth/source/oauth2"
  26. repo_service "code.gitea.io/gitea/services/repository"
  27. user_service "code.gitea.io/gitea/services/user"
  28. "github.com/urfave/cli"
  29. )
  30. var (
  31. // CmdAdmin represents the available admin sub-command.
  32. CmdAdmin = cli.Command{
  33. Name: "admin",
  34. Usage: "Command line interface to perform common administrative operations",
  35. Subcommands: []cli.Command{
  36. subcmdUser,
  37. subcmdRepoSyncReleases,
  38. subcmdRegenerate,
  39. subcmdAuth,
  40. subcmdSendMail,
  41. },
  42. }
  43. subcmdUser = cli.Command{
  44. Name: "user",
  45. Usage: "Modify users",
  46. Subcommands: []cli.Command{
  47. microcmdUserCreate,
  48. microcmdUserList,
  49. microcmdUserChangePassword,
  50. microcmdUserDelete,
  51. },
  52. }
  53. microcmdUserList = cli.Command{
  54. Name: "list",
  55. Usage: "List users",
  56. Action: runListUsers,
  57. Flags: []cli.Flag{
  58. cli.BoolFlag{
  59. Name: "admin",
  60. Usage: "List only admin users",
  61. },
  62. },
  63. }
  64. microcmdUserCreate = cli.Command{
  65. Name: "create",
  66. Usage: "Create a new user in database",
  67. Action: runCreateUser,
  68. Flags: []cli.Flag{
  69. cli.StringFlag{
  70. Name: "name",
  71. Usage: "Username. DEPRECATED: use username instead",
  72. },
  73. cli.StringFlag{
  74. Name: "username",
  75. Usage: "Username",
  76. },
  77. cli.StringFlag{
  78. Name: "password",
  79. Usage: "User password",
  80. },
  81. cli.StringFlag{
  82. Name: "email",
  83. Usage: "User email address",
  84. },
  85. cli.BoolFlag{
  86. Name: "admin",
  87. Usage: "User is an admin",
  88. },
  89. cli.BoolFlag{
  90. Name: "random-password",
  91. Usage: "Generate a random password for the user",
  92. },
  93. cli.BoolFlag{
  94. Name: "must-change-password",
  95. Usage: "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)",
  96. },
  97. cli.IntFlag{
  98. Name: "random-password-length",
  99. Usage: "Length of the random password to be generated",
  100. Value: 12,
  101. },
  102. cli.BoolFlag{
  103. Name: "access-token",
  104. Usage: "Generate access token for the user",
  105. },
  106. },
  107. }
  108. microcmdUserChangePassword = cli.Command{
  109. Name: "change-password",
  110. Usage: "Change a user's password",
  111. Action: runChangePassword,
  112. Flags: []cli.Flag{
  113. cli.StringFlag{
  114. Name: "username,u",
  115. Value: "",
  116. Usage: "The user to change password for",
  117. },
  118. cli.StringFlag{
  119. Name: "password,p",
  120. Value: "",
  121. Usage: "New password to set for user",
  122. },
  123. },
  124. }
  125. microcmdUserDelete = cli.Command{
  126. Name: "delete",
  127. Usage: "Delete specific user by id, name or email",
  128. Flags: []cli.Flag{
  129. cli.Int64Flag{
  130. Name: "id",
  131. Usage: "ID of user of the user to delete",
  132. },
  133. cli.StringFlag{
  134. Name: "username,u",
  135. Usage: "Username of the user to delete",
  136. },
  137. cli.StringFlag{
  138. Name: "email,e",
  139. Usage: "Email of the user to delete",
  140. },
  141. },
  142. Action: runDeleteUser,
  143. }
  144. subcmdRepoSyncReleases = cli.Command{
  145. Name: "repo-sync-releases",
  146. Usage: "Synchronize repository releases with tags",
  147. Action: runRepoSyncReleases,
  148. }
  149. subcmdRegenerate = cli.Command{
  150. Name: "regenerate",
  151. Usage: "Regenerate specific files",
  152. Subcommands: []cli.Command{
  153. microcmdRegenHooks,
  154. microcmdRegenKeys,
  155. },
  156. }
  157. microcmdRegenHooks = cli.Command{
  158. Name: "hooks",
  159. Usage: "Regenerate git-hooks",
  160. Action: runRegenerateHooks,
  161. }
  162. microcmdRegenKeys = cli.Command{
  163. Name: "keys",
  164. Usage: "Regenerate authorized_keys file",
  165. Action: runRegenerateKeys,
  166. }
  167. subcmdAuth = cli.Command{
  168. Name: "auth",
  169. Usage: "Modify external auth providers",
  170. Subcommands: []cli.Command{
  171. microcmdAuthAddOauth,
  172. microcmdAuthUpdateOauth,
  173. cmdAuthAddLdapBindDn,
  174. cmdAuthUpdateLdapBindDn,
  175. cmdAuthAddLdapSimpleAuth,
  176. cmdAuthUpdateLdapSimpleAuth,
  177. microcmdAuthList,
  178. microcmdAuthDelete,
  179. },
  180. }
  181. microcmdAuthList = cli.Command{
  182. Name: "list",
  183. Usage: "List auth sources",
  184. Action: runListAuth,
  185. Flags: []cli.Flag{
  186. cli.IntFlag{
  187. Name: "min-width",
  188. Usage: "Minimal cell width including any padding for the formatted table",
  189. Value: 0,
  190. },
  191. cli.IntFlag{
  192. Name: "tab-width",
  193. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  194. Value: 8,
  195. },
  196. cli.IntFlag{
  197. Name: "padding",
  198. Usage: "padding added to a cell before computing its width",
  199. Value: 1,
  200. },
  201. cli.StringFlag{
  202. Name: "pad-char",
  203. Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`,
  204. Value: "\t",
  205. },
  206. cli.BoolFlag{
  207. Name: "vertical-bars",
  208. Usage: "Set to true to print vertical bars between columns",
  209. },
  210. },
  211. }
  212. idFlag = cli.Int64Flag{
  213. Name: "id",
  214. Usage: "ID of authentication source",
  215. }
  216. microcmdAuthDelete = cli.Command{
  217. Name: "delete",
  218. Usage: "Delete specific auth source",
  219. Flags: []cli.Flag{idFlag},
  220. Action: runDeleteAuth,
  221. }
  222. oauthCLIFlags = []cli.Flag{
  223. cli.StringFlag{
  224. Name: "name",
  225. Value: "",
  226. Usage: "Application Name",
  227. },
  228. cli.StringFlag{
  229. Name: "provider",
  230. Value: "",
  231. Usage: "OAuth2 Provider",
  232. },
  233. cli.StringFlag{
  234. Name: "key",
  235. Value: "",
  236. Usage: "Client ID (Key)",
  237. },
  238. cli.StringFlag{
  239. Name: "secret",
  240. Value: "",
  241. Usage: "Client Secret",
  242. },
  243. cli.StringFlag{
  244. Name: "auto-discover-url",
  245. Value: "",
  246. Usage: "OpenID Connect Auto Discovery URL (only required when using OpenID Connect as provider)",
  247. },
  248. cli.StringFlag{
  249. Name: "use-custom-urls",
  250. Value: "false",
  251. Usage: "Use custom URLs for GitLab/GitHub OAuth endpoints",
  252. },
  253. cli.StringFlag{
  254. Name: "custom-auth-url",
  255. Value: "",
  256. Usage: "Use a custom Authorization URL (option for GitLab/GitHub)",
  257. },
  258. cli.StringFlag{
  259. Name: "custom-token-url",
  260. Value: "",
  261. Usage: "Use a custom Token URL (option for GitLab/GitHub)",
  262. },
  263. cli.StringFlag{
  264. Name: "custom-profile-url",
  265. Value: "",
  266. Usage: "Use a custom Profile URL (option for GitLab/GitHub)",
  267. },
  268. cli.StringFlag{
  269. Name: "custom-email-url",
  270. Value: "",
  271. Usage: "Use a custom Email URL (option for GitHub)",
  272. },
  273. cli.StringFlag{
  274. Name: "icon-url",
  275. Value: "",
  276. Usage: "Custom icon URL for OAuth2 login source",
  277. },
  278. cli.BoolFlag{
  279. Name: "skip-local-2fa",
  280. Usage: "Set to true to skip local 2fa for users authenticated by this source",
  281. },
  282. }
  283. microcmdAuthUpdateOauth = cli.Command{
  284. Name: "update-oauth",
  285. Usage: "Update existing Oauth authentication source",
  286. Action: runUpdateOauth,
  287. Flags: append(oauthCLIFlags[:1], append([]cli.Flag{idFlag}, oauthCLIFlags[1:]...)...),
  288. }
  289. microcmdAuthAddOauth = cli.Command{
  290. Name: "add-oauth",
  291. Usage: "Add new Oauth authentication source",
  292. Action: runAddOauth,
  293. Flags: oauthCLIFlags,
  294. }
  295. subcmdSendMail = cli.Command{
  296. Name: "sendmail",
  297. Usage: "Send a message to all users",
  298. Action: runSendMail,
  299. Flags: []cli.Flag{
  300. cli.StringFlag{
  301. Name: "title",
  302. Usage: `a title of a message`,
  303. Value: "",
  304. },
  305. cli.StringFlag{
  306. Name: "content",
  307. Usage: "a content of a message",
  308. Value: "",
  309. },
  310. cli.BoolFlag{
  311. Name: "force,f",
  312. Usage: "A flag to bypass a confirmation step",
  313. },
  314. },
  315. }
  316. )
  317. func runChangePassword(c *cli.Context) error {
  318. if err := argsSet(c, "username", "password"); err != nil {
  319. return err
  320. }
  321. ctx, cancel := installSignals()
  322. defer cancel()
  323. if err := initDB(ctx); err != nil {
  324. return err
  325. }
  326. if !pwd.IsComplexEnough(c.String("password")) {
  327. return errors.New("Password does not meet complexity requirements")
  328. }
  329. pwned, err := pwd.IsPwned(context.Background(), c.String("password"))
  330. if err != nil {
  331. return err
  332. }
  333. if pwned {
  334. return errors.New("The password you chose is on a list of stolen passwords previously exposed in public data breaches. Please try again with a different password.\nFor more details, see https://haveibeenpwned.com/Passwords")
  335. }
  336. uname := c.String("username")
  337. user, err := user_model.GetUserByName(uname)
  338. if err != nil {
  339. return err
  340. }
  341. if err = user.SetPassword(c.String("password")); err != nil {
  342. return err
  343. }
  344. if err = user_model.UpdateUserCols(db.DefaultContext, user, "passwd", "passwd_hash_algo", "salt"); err != nil {
  345. return err
  346. }
  347. fmt.Printf("%s's password has been successfully updated!\n", user.Name)
  348. return nil
  349. }
  350. func runCreateUser(c *cli.Context) error {
  351. if err := argsSet(c, "email"); err != nil {
  352. return err
  353. }
  354. if c.IsSet("name") && c.IsSet("username") {
  355. return errors.New("Cannot set both --name and --username flags")
  356. }
  357. if !c.IsSet("name") && !c.IsSet("username") {
  358. return errors.New("One of --name or --username flags must be set")
  359. }
  360. if c.IsSet("password") && c.IsSet("random-password") {
  361. return errors.New("cannot set both -random-password and -password flags")
  362. }
  363. var username string
  364. if c.IsSet("username") {
  365. username = c.String("username")
  366. } else {
  367. username = c.String("name")
  368. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  369. }
  370. ctx, cancel := installSignals()
  371. defer cancel()
  372. if err := initDB(ctx); err != nil {
  373. return err
  374. }
  375. var password string
  376. if c.IsSet("password") {
  377. password = c.String("password")
  378. } else if c.IsSet("random-password") {
  379. var err error
  380. password, err = pwd.Generate(c.Int("random-password-length"))
  381. if err != nil {
  382. return err
  383. }
  384. fmt.Printf("generated random password is '%s'\n", password)
  385. } else {
  386. return errors.New("must set either password or random-password flag")
  387. }
  388. // always default to true
  389. var changePassword = true
  390. // If this is the first user being created.
  391. // Take it as the admin and don't force a password update.
  392. if n := user_model.CountUsers(); n == 0 {
  393. changePassword = false
  394. }
  395. if c.IsSet("must-change-password") {
  396. changePassword = c.Bool("must-change-password")
  397. }
  398. u := &user_model.User{
  399. Name: username,
  400. Email: c.String("email"),
  401. Passwd: password,
  402. IsActive: true,
  403. IsAdmin: c.Bool("admin"),
  404. MustChangePassword: changePassword,
  405. Theme: setting.UI.DefaultTheme,
  406. }
  407. if err := user_model.CreateUser(u); err != nil {
  408. return fmt.Errorf("CreateUser: %v", err)
  409. }
  410. if c.Bool("access-token") {
  411. t := &models.AccessToken{
  412. Name: "gitea-admin",
  413. UID: u.ID,
  414. }
  415. if err := models.NewAccessToken(t); err != nil {
  416. return err
  417. }
  418. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  419. }
  420. fmt.Printf("New user '%s' has been successfully created!\n", username)
  421. return nil
  422. }
  423. func runListUsers(c *cli.Context) error {
  424. ctx, cancel := installSignals()
  425. defer cancel()
  426. if err := initDB(ctx); err != nil {
  427. return err
  428. }
  429. users, err := user_model.GetAllUsers()
  430. if err != nil {
  431. return err
  432. }
  433. w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
  434. if c.IsSet("admin") {
  435. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
  436. for _, u := range users {
  437. if u.IsAdmin {
  438. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
  439. }
  440. }
  441. } else {
  442. fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
  443. for _, u := range users {
  444. fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin)
  445. }
  446. }
  447. w.Flush()
  448. return nil
  449. }
  450. func runDeleteUser(c *cli.Context) error {
  451. if !c.IsSet("id") && !c.IsSet("username") && !c.IsSet("email") {
  452. return fmt.Errorf("You must provide the id, username or email of a user to delete")
  453. }
  454. ctx, cancel := installSignals()
  455. defer cancel()
  456. if err := initDB(ctx); err != nil {
  457. return err
  458. }
  459. if err := storage.Init(); err != nil {
  460. return err
  461. }
  462. var err error
  463. var user *user_model.User
  464. if c.IsSet("email") {
  465. user, err = user_model.GetUserByEmail(c.String("email"))
  466. } else if c.IsSet("username") {
  467. user, err = user_model.GetUserByName(c.String("username"))
  468. } else {
  469. user, err = user_model.GetUserByID(c.Int64("id"))
  470. }
  471. if err != nil {
  472. return err
  473. }
  474. if c.IsSet("username") && user.LowerName != strings.ToLower(strings.TrimSpace(c.String("username"))) {
  475. return fmt.Errorf("The user %s who has email %s does not match the provided username %s", user.Name, c.String("email"), c.String("username"))
  476. }
  477. if c.IsSet("id") && user.ID != c.Int64("id") {
  478. return fmt.Errorf("The user %s does not match the provided id %d", user.Name, c.Int64("id"))
  479. }
  480. return user_service.DeleteUser(user)
  481. }
  482. func runRepoSyncReleases(_ *cli.Context) error {
  483. ctx, cancel := installSignals()
  484. defer cancel()
  485. if err := initDB(ctx); err != nil {
  486. return err
  487. }
  488. log.Trace("Synchronizing repository releases (this may take a while)")
  489. for page := 1; ; page++ {
  490. repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{
  491. ListOptions: db.ListOptions{
  492. PageSize: models.RepositoryListDefaultPageSize,
  493. Page: page,
  494. },
  495. Private: true,
  496. })
  497. if err != nil {
  498. return fmt.Errorf("SearchRepositoryByName: %v", err)
  499. }
  500. if len(repos) == 0 {
  501. break
  502. }
  503. log.Trace("Processing next %d repos of %d", len(repos), count)
  504. for _, repo := range repos {
  505. log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
  506. gitRepo, err := git.OpenRepository(repo.RepoPath())
  507. if err != nil {
  508. log.Warn("OpenRepository: %v", err)
  509. continue
  510. }
  511. oldnum, err := getReleaseCount(repo.ID)
  512. if err != nil {
  513. log.Warn(" GetReleaseCountByRepoID: %v", err)
  514. }
  515. log.Trace(" currentNumReleases is %d, running SyncReleasesWithTags", oldnum)
  516. if err = repo_module.SyncReleasesWithTags(repo, gitRepo); err != nil {
  517. log.Warn(" SyncReleasesWithTags: %v", err)
  518. gitRepo.Close()
  519. continue
  520. }
  521. count, err = getReleaseCount(repo.ID)
  522. if err != nil {
  523. log.Warn(" GetReleaseCountByRepoID: %v", err)
  524. gitRepo.Close()
  525. continue
  526. }
  527. log.Trace(" repo %s releases synchronized to tags: from %d to %d",
  528. repo.FullName(), oldnum, count)
  529. gitRepo.Close()
  530. }
  531. }
  532. return nil
  533. }
  534. func getReleaseCount(id int64) (int64, error) {
  535. return models.GetReleaseCountByRepoID(
  536. id,
  537. models.FindReleasesOptions{
  538. IncludeTags: true,
  539. },
  540. )
  541. }
  542. func runRegenerateHooks(_ *cli.Context) error {
  543. ctx, cancel := installSignals()
  544. defer cancel()
  545. if err := initDB(ctx); err != nil {
  546. return err
  547. }
  548. return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
  549. }
  550. func runRegenerateKeys(_ *cli.Context) error {
  551. ctx, cancel := installSignals()
  552. defer cancel()
  553. if err := initDB(ctx); err != nil {
  554. return err
  555. }
  556. return models.RewriteAllPublicKeys()
  557. }
  558. func parseOAuth2Config(c *cli.Context) *oauth2.Source {
  559. var customURLMapping *oauth2.CustomURLMapping
  560. if c.IsSet("use-custom-urls") {
  561. customURLMapping = &oauth2.CustomURLMapping{
  562. TokenURL: c.String("custom-token-url"),
  563. AuthURL: c.String("custom-auth-url"),
  564. ProfileURL: c.String("custom-profile-url"),
  565. EmailURL: c.String("custom-email-url"),
  566. }
  567. } else {
  568. customURLMapping = nil
  569. }
  570. return &oauth2.Source{
  571. Provider: c.String("provider"),
  572. ClientID: c.String("key"),
  573. ClientSecret: c.String("secret"),
  574. OpenIDConnectAutoDiscoveryURL: c.String("auto-discover-url"),
  575. CustomURLMapping: customURLMapping,
  576. IconURL: c.String("icon-url"),
  577. SkipLocalTwoFA: c.Bool("skip-local-2fa"),
  578. }
  579. }
  580. func runAddOauth(c *cli.Context) error {
  581. ctx, cancel := installSignals()
  582. defer cancel()
  583. if err := initDB(ctx); err != nil {
  584. return err
  585. }
  586. return login.CreateSource(&login.Source{
  587. Type: login.OAuth2,
  588. Name: c.String("name"),
  589. IsActive: true,
  590. Cfg: parseOAuth2Config(c),
  591. })
  592. }
  593. func runUpdateOauth(c *cli.Context) error {
  594. if !c.IsSet("id") {
  595. return fmt.Errorf("--id flag is missing")
  596. }
  597. ctx, cancel := installSignals()
  598. defer cancel()
  599. if err := initDB(ctx); err != nil {
  600. return err
  601. }
  602. source, err := login.GetSourceByID(c.Int64("id"))
  603. if err != nil {
  604. return err
  605. }
  606. oAuth2Config := source.Cfg.(*oauth2.Source)
  607. if c.IsSet("name") {
  608. source.Name = c.String("name")
  609. }
  610. if c.IsSet("provider") {
  611. oAuth2Config.Provider = c.String("provider")
  612. }
  613. if c.IsSet("key") {
  614. oAuth2Config.ClientID = c.String("key")
  615. }
  616. if c.IsSet("secret") {
  617. oAuth2Config.ClientSecret = c.String("secret")
  618. }
  619. if c.IsSet("auto-discover-url") {
  620. oAuth2Config.OpenIDConnectAutoDiscoveryURL = c.String("auto-discover-url")
  621. }
  622. if c.IsSet("icon-url") {
  623. oAuth2Config.IconURL = c.String("icon-url")
  624. }
  625. // update custom URL mapping
  626. var customURLMapping = &oauth2.CustomURLMapping{}
  627. if oAuth2Config.CustomURLMapping != nil {
  628. customURLMapping.TokenURL = oAuth2Config.CustomURLMapping.TokenURL
  629. customURLMapping.AuthURL = oAuth2Config.CustomURLMapping.AuthURL
  630. customURLMapping.ProfileURL = oAuth2Config.CustomURLMapping.ProfileURL
  631. customURLMapping.EmailURL = oAuth2Config.CustomURLMapping.EmailURL
  632. }
  633. if c.IsSet("use-custom-urls") && c.IsSet("custom-token-url") {
  634. customURLMapping.TokenURL = c.String("custom-token-url")
  635. }
  636. if c.IsSet("use-custom-urls") && c.IsSet("custom-auth-url") {
  637. customURLMapping.AuthURL = c.String("custom-auth-url")
  638. }
  639. if c.IsSet("use-custom-urls") && c.IsSet("custom-profile-url") {
  640. customURLMapping.ProfileURL = c.String("custom-profile-url")
  641. }
  642. if c.IsSet("use-custom-urls") && c.IsSet("custom-email-url") {
  643. customURLMapping.EmailURL = c.String("custom-email-url")
  644. }
  645. oAuth2Config.CustomURLMapping = customURLMapping
  646. source.Cfg = oAuth2Config
  647. return login.UpdateSource(source)
  648. }
  649. func runListAuth(c *cli.Context) error {
  650. ctx, cancel := installSignals()
  651. defer cancel()
  652. if err := initDB(ctx); err != nil {
  653. return err
  654. }
  655. loginSources, err := login.Sources()
  656. if err != nil {
  657. return err
  658. }
  659. flags := tabwriter.AlignRight
  660. if c.Bool("vertical-bars") {
  661. flags |= tabwriter.Debug
  662. }
  663. padChar := byte('\t')
  664. if len(c.String("pad-char")) > 0 {
  665. padChar = c.String("pad-char")[0]
  666. }
  667. // loop through each source and print
  668. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  669. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  670. for _, source := range loginSources {
  671. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
  672. }
  673. w.Flush()
  674. return nil
  675. }
  676. func runDeleteAuth(c *cli.Context) error {
  677. if !c.IsSet("id") {
  678. return fmt.Errorf("--id flag is missing")
  679. }
  680. ctx, cancel := installSignals()
  681. defer cancel()
  682. if err := initDB(ctx); err != nil {
  683. return err
  684. }
  685. source, err := login.GetSourceByID(c.Int64("id"))
  686. if err != nil {
  687. return err
  688. }
  689. return auth_service.DeleteLoginSource(source)
  690. }