Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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