Add support for Gitea authentication

Show dashboard
Set session store to file

Signed-off-by: Jonas Franz <info@jonasfranz.software>
web-ui
Jonas Franz 6 years ago
parent 2f5e24d9cc
commit b1670e92bf
Signed by untrusted user: JonasFranzDEV
GPG Key ID: 506AEEBE80BEDECD
  1. 1
      .gitignore
  2. 13
      Gopkg.lock
  3. 7
      Gopkg.toml
  4. 46
      web/auth/gitea.go
  5. 12
      web/context/context.go
  6. 22
      web/router.go
  7. 5
      web/templates/base/head.tmpl
  8. 32
      web/templates/dashboard.tmpl
  9. 67
      web/templates/login_gitea.tmpl
  10. 22
      web/templates/migrate.tmpl

1
.gitignore vendored

@ -15,3 +15,4 @@
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
data/

13
Gopkg.lock generated

@ -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

@ -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"

@ -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
}

@ -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)
}
}

@ -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
}

@ -65,4 +65,7 @@
;
</script>
</head>
<body>
<body>
{{if .Flash.ErrorMsg}}
<div class="ui error message">{{.Flash.ErrorMsg}}</div>
{{end}}

@ -0,0 +1,32 @@
{{template "base/head" .}}
<div class="ui middle aligned center aligned grid">
<div class="column">
<h1 class="ui image header">
<div class="content">
Gitea Migrator
</div>
</h1>
<div class="ui message">
You've connected your GitHub and Gitea account.
</div>
<div class="ui icon message">
<i class="icon github"></i>
<div class="content">
<h3 class="header">GitHub connected</h3>
You're logged in as <code>{{.User.Username}}</code>. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui icon attached message">
<i class="icon lock"></i>
<div class="content">
<h3 class="header">Gitea connected</h3>
You're logged in as <code>{{.GiteaUser.Username}}</code>. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui stacked segment">
<button class="ui fluid large green labeld icon button"><i class="icon list"></i> Migrate Repositories...</button>
</div>
</div>
</div>
{{template "base/footer" .}}

@ -0,0 +1,67 @@
{{template "base/head" .}}
<div class="ui middle aligned center aligned grid">
<div class="column">
<h1 class="ui image header">
<div class="content">
Gitea Migrator
</div>
</h1>
<div class="ui message">
You've connected you GitHub account. The next step is to connect to your Gitea instance.
</div>
<div class="ui icon message">
<i class="icon github"></i>
<div class="content">
<h3 class="header">GitHub connected</h3>
You're logged in as <code>{{.User.Username}}</code>. <a href="/logout">Not you?</a>
</div>
</div>
<div class="ui icon attached message">
<i class="icon lock"></i>
<div class="content">
<h3 class="header">Login to Gitea</h3>
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.
</div>
</div>
<form action="/gitea" method="POST" class="ui large form">
<div class="ui attached fluid segment">
<div class="field">
<label>Gitea URL</label>
<input name="gitea-url" type="url">
</div>
<h3>Credentials</h3>
<div class="ui top attached tabular menu">
<a class="active item" data-tab="password">Username + Password</a>
<a class="item" data-tab="access-token">Access Token</a>
</div>
<div class="ui bottom attached tab segment active" data-tab="password">
<div class="field">
<label>Username</label>
<input name="username" placeholder="Username" type="text">
</div>
<div class="field">
<label>Password</label>
<input name="password" type="password">
</div>
<button type="submit" name="use" value="password" class="ui fluid large green submit button">Login to Gitea</button>
</div>
<div class="ui bottom attached tab segment" data-tab="access-token">
<div class="field">
<label>Access Token</label>
<input name="access-token" type="password">
</div>
<button type="submit" name="use" value="token" class="ui fluid large green submit button">Login to Gitea</button>
</div>
</div>
<div class="ui error message"></div>
</form>
</div>
</div>
<script>
$('.menu .item')
.tab()
;
</script>
{{template "base/footer" .}}

@ -1,22 +0,0 @@
{{template "base/head" .}}
<div class="ui middle aligned center aligned grid">
<div class="column">
<h1 class="ui image header">
<div class="content">
Welcome {{.User.Username}}
</div>
</h1>
<div class="ui message">
Migrate all your GitHub repositories to your Gitea instance including all issues, labels and milestones.
</div>
<form class="ui large form">
<div class="ui stacked segment">
<a href="/github" class="ui fluid large green submit labeled icon button"><i class="icon github"></i> Login with GitHub</a>
</div>
<div class="ui error message"></div>
</form>
</div>
</div>
{{template "base/footer" .}}
Loading…
Cancel
Save