diff --git a/.gitignore b/.gitignore index 81ca976..088d441 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736 .glide/ +data/ diff --git a/Gopkg.lock b/Gopkg.lock index 5baa9bb..7f36751 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -2,10 +2,11 @@ [[projects]] - branch = "master" + branch = "myuserinfo" name = "code.gitea.io/sdk" packages = ["gitea"] - revision = "b2308e3f700875a3642a78bd3f6e5db8ef6f974d" + revision = "6c33946d3f5b59646286ec6d6f02e88de690a1b3" + source = "github.com/JonasFranzDEV/go-sdk" [[projects]] name = "github.com/BurntSushi/toml" @@ -25,6 +26,12 @@ packages = ["."] revision = "88fd7c52a2c704c5d530718c1be454292a806e2b" +[[projects]] + branch = "master" + name = "github.com/go-macaron/binding" + packages = ["."] + revision = "ac54ee249c27dca7e76fad851a4a04b73bd1b183" + [[projects]] branch = "master" name = "github.com/go-macaron/inject" @@ -139,6 +146,6 @@ [solve-meta] analyzer-name = "dep" analyzer-version = 1 - inputs-digest = "88f88bb5625ff2b06fff92a8a4c8ddebb4a393c05dc4b9ecbf02d0079aab79c3" + inputs-digest = "10db78bccc75a090bb1e23467c4f5d6abb8bd3b4b158e4b684550539d23565c9" solver-name = "gps-cdcl" solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml index 8974b36..bb32991 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -30,8 +30,9 @@ unused-packages = true [[constraint]] - branch = "master" + branch = "myuserinfo" name = "code.gitea.io/sdk" + source = "github.com/JonasFranzDEV/go-sdk" [[constraint]] name = "github.com/urfave/cli" @@ -60,3 +61,7 @@ [[constraint]] branch = "master" name = "github.com/go-macaron/session" + +[[constraint]] + branch = "master" + name = "github.com/go-macaron/binding" \ No newline at end of file diff --git a/web/auth/gitea.go b/web/auth/gitea.go new file mode 100644 index 0000000..25184b5 --- /dev/null +++ b/web/auth/gitea.go @@ -0,0 +1,46 @@ +package auth + +import ( + "code.gitea.io/sdk/gitea" + "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/context" +) + +type GiteaLoginForm struct { + Username string `form:"username"` + Password string `form:"password"` + AccessToken string `form:"access-token"` + GiteaURL string `form:"gitea-url"` + Type string `form:"use" binding:"Required;In(token,password)"` +} + +func LoginToGitea(ctx *context.Context, form GiteaLoginForm) { + var token string + if form.Type == "password" { + client := gitea.NewClient(form.GiteaURL, "") + tkn, err := client.CreateAccessToken(form.Username, form.Password, gitea.CreateAccessTokenOption{ + Name: "gitea-github-migrator", + }) + if err != nil { + ctx.Flash.Error("Cannot create access token please check your credentials!") + ctx.Redirect("/") + return + } + token = tkn.Sha1 + } else { + token = form.AccessToken + } + client := gitea.NewClient(form.GiteaURL, token) + usr, err := client.GetMyUserInfo() + if err != nil { + ctx.Flash.Error("Invalid Gitea credentials.") + ctx.Redirect("/") + return + } + ctx.Session.Set("gitea_user", &context.User{ + Username: usr.UserName, + Token: token, + AvatarURL: usr.AvatarURL, + }) + ctx.Redirect("/") + return +} diff --git a/web/context/context.go b/web/context/context.go index bc621b2..ade7780 100644 --- a/web/context/context.go +++ b/web/context/context.go @@ -16,9 +16,10 @@ type Context struct { Flash *session.Flash Session session.Store - Client *github.Client - User *User - Link string // current request URL + Client *github.Client + User *User //GitHub user + GiteaUser *User + Link string // current request URL } type User struct { @@ -68,6 +69,11 @@ func Contexter() macaron.Handler { ctx.User = usr.(*User) ctx.Data["User"] = ctx.User } + gitea_usr := sess.Get("gitea_user") + if gitea_usr != nil { + ctx.GiteaUser = gitea_usr.(*User) + ctx.Data["GiteaUser"] = ctx.GiteaUser + } c.Map(ctx) } } diff --git a/web/router.go b/web/router.go index 02a95b3..b18f73f 100644 --- a/web/router.go +++ b/web/router.go @@ -1,8 +1,11 @@ package web import ( + "encoding/gob" + "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/auth" "git.jonasfranz.software/JonasFranzDEV/gitea-github-migrator/web/context" + "github.com/go-macaron/binding" "github.com/go-macaron/session" "github.com/gobuffalo/packr" "gopkg.in/macaron.v1" @@ -10,12 +13,16 @@ import ( // InitRoutes initiates the gin routes and loads values from config func InitRoutes() *macaron.Macaron { + gob.Register(&context.User{}) m := macaron.Classic() auth.InitGitHubOAuthConfig() tmplBox := packr.NewBox("templates") publicBox := packr.NewBox("public") m.Use(macaron.Recovery()) - m.Use(session.Sessioner()) + m.Use(session.Sessioner(session.Options{ + Provider: "file", + ProviderConfig: "data/sessions", + })) m.Use(macaron.Renderer(macaron.RenderOptions{ TemplateFileSystem: &BundledFS{tmplBox}, })) @@ -26,14 +33,25 @@ func InitRoutes() *macaron.Macaron { m.Use(context.Contexter()) m.Get("/", func(ctx *context.Context) { if ctx.User != nil { - ctx.HTML(200, "migrate") + if ctx.GiteaUser == nil { + ctx.HTML(200, "login_gitea") + return + } + ctx.HTML(200, "dashboard") return } ctx.HTML(200, "login_github") // 200 is the response code. }) + m.Get("/logout", func(c *macaron.Context, sess session.Store) { + sess.Destory(c) + c.Redirect("/") + }) m.Group("/github", func() { m.Get("/", auth.RedirectToGitHub) m.Get("/callback", auth.CallbackFromGitHub) }) + m.Group("/gitea", func() { + m.Post("/", binding.BindIgnErr(auth.GiteaLoginForm{}), auth.LoginToGitea) + }) return m } diff --git a/web/templates/base/head.tmpl b/web/templates/base/head.tmpl index 0dae18f..675b101 100644 --- a/web/templates/base/head.tmpl +++ b/web/templates/base/head.tmpl @@ -65,4 +65,7 @@ ; - \ No newline at end of file + +{{if .Flash.ErrorMsg}} +
{{.Flash.ErrorMsg}}
+{{end}} \ No newline at end of file diff --git a/web/templates/dashboard.tmpl b/web/templates/dashboard.tmpl new file mode 100644 index 0000000..89cb76e --- /dev/null +++ b/web/templates/dashboard.tmpl @@ -0,0 +1,32 @@ +{{template "base/head" .}} +
+
+

+
+ Gitea Migrator +
+

+
+ You've connected your GitHub and Gitea account. +
+
+ +
+

GitHub connected

+ You're logged in as {{.User.Username}}. Not you? +
+
+
+ +
+

Gitea connected

+ You're logged in as {{.GiteaUser.Username}}. Not you? +
+
+ +
+ +
+
+
+{{template "base/footer" .}} \ No newline at end of file diff --git a/web/templates/login_gitea.tmpl b/web/templates/login_gitea.tmpl new file mode 100644 index 0000000..b058617 --- /dev/null +++ b/web/templates/login_gitea.tmpl @@ -0,0 +1,67 @@ +{{template "base/head" .}} +
+
+

+
+ Gitea Migrator +
+

+
+ You've connected you GitHub account. The next step is to connect to your Gitea instance. +
+
+ +
+

GitHub connected

+ You're logged in as {{.User.Username}}. Not you? +
+
+
+ +
+

Login to Gitea

+ You can use your user credentials or an access token to log in to your Gitea instance. If you use your credentials + an access token will be created for you. +
+
+
+
+
+ + +
+

Credentials

+ +
+
+ + +
+
+ + +
+ +
+
+
+ + +
+ +
+
+ +
+
+
+
+ +{{template "base/footer" .}} \ No newline at end of file diff --git a/web/templates/migrate.tmpl b/web/templates/migrate.tmpl deleted file mode 100644 index f6c1119..0000000 --- a/web/templates/migrate.tmpl +++ /dev/null @@ -1,22 +0,0 @@ -{{template "base/head" .}} -
-
-

-
- Welcome {{.User.Username}} -
-

-
- Migrate all your GitHub repositories to your Gitea instance including all issues, labels and milestones. -
-
- - -
- -
-
-
-{{template "base/footer" .}} \ No newline at end of file