1
0
Fork 0
forked from forgejo/forgejo

Fix issue with log in with GitHub but need more error handle after

This commit is contained in:
Unknown 2014-04-07 12:56:40 -04:00
parent 05fb34eacd
commit 9ea9818d32
13 changed files with 167 additions and 60 deletions

View file

@ -22,13 +22,21 @@ import (
"github.com/gogits/gogs/modules/log"
)
// Mailer represents a mail service.
// Mailer represents mail service.
type Mailer struct {
Name string
Host string
User, Passwd string
}
// Oauther represents oauth service.
type Oauther struct {
GitHub struct {
Enabled bool
ClientId, ClientSecret string
}
}
var (
AppVer string
AppName string
@ -45,8 +53,9 @@ var (
CookieUserName string
CookieRememberName string
Cfg *goconfig.ConfigFile
MailService *Mailer
Cfg *goconfig.ConfigFile
MailService *Mailer
OauthService *Oauther
LogMode string
LogConfig string
@ -206,15 +215,17 @@ func newSessionService() {
func newMailService() {
// Check mailer setting.
if Cfg.MustBool("mailer", "ENABLED") {
MailService = &Mailer{
Name: Cfg.MustValue("mailer", "NAME", AppName),
Host: Cfg.MustValue("mailer", "HOST"),
User: Cfg.MustValue("mailer", "USER"),
Passwd: Cfg.MustValue("mailer", "PASSWD"),
}
log.Info("Mail Service Enabled")
if !Cfg.MustBool("mailer", "ENABLED") {
return
}
MailService = &Mailer{
Name: Cfg.MustValue("mailer", "NAME", AppName),
Host: Cfg.MustValue("mailer", "HOST"),
User: Cfg.MustValue("mailer", "USER"),
Passwd: Cfg.MustValue("mailer", "PASSWD"),
}
log.Info("Mail Service Enabled")
}
func newRegisterMailService() {
@ -239,6 +250,25 @@ func newNotifyMailService() {
log.Info("Notify Mail Service Enabled")
}
func newOauthService() {
if !Cfg.MustBool("oauth", "ENABLED") {
return
}
OauthService = &Oauther{}
oauths := make([]string, 0, 10)
// GitHub.
if Cfg.MustBool("oauth.github", "ENABLED") {
OauthService.GitHub.Enabled = true
OauthService.GitHub.ClientId = Cfg.MustValue("oauth.github", "CLIENT_ID")
OauthService.GitHub.ClientSecret = Cfg.MustValue("oauth.github", "CLIENT_SECRET")
oauths = append(oauths, "GitHub")
}
log.Info("Oauth Service Enabled %s", oauths)
}
func NewConfigContext() {
//var err error
workDir, err := ExecDir()
@ -303,4 +333,5 @@ func NewServices() {
newMailService()
newRegisterMailService()
newNotifyMailService()
newOauthService()
}

View file

@ -90,21 +90,21 @@ func (options *CustomRender) Link(out *bytes.Buffer, link []byte, title []byte,
}
var (
mentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`)
MentionPattern = regexp.MustCompile(`@[0-9a-zA-Z_]{1,}`)
commitPattern = regexp.MustCompile(`(\s|^)https?.*commit/[0-9a-zA-Z]+(#+[0-9a-zA-Z-]*)?`)
issueFullPattern = regexp.MustCompile(`(\s|^)https?.*issues/[0-9]+(#+[0-9a-zA-Z-]*)?`)
issueIndexPattern = regexp.MustCompile(`(\s|^)#[0-9]+`)
issueIndexPattern = regexp.MustCompile(`#[0-9]+`)
)
func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
ms := mentionPattern.FindAll(rawBytes, -1)
ms := MentionPattern.FindAll(rawBytes, -1)
for _, m := range ms {
rawBytes = bytes.Replace(rawBytes, m,
[]byte(fmt.Sprintf(`<a href="/user/%s">%s</a>`, m[1:], m)), -1)
}
ms = commitPattern.FindAll(rawBytes, -1)
for _, m := range ms {
m = bytes.TrimPrefix(m, []byte(" "))
m = bytes.TrimSpace(m)
i := strings.Index(string(m), "commit/")
j := strings.Index(string(m), "#")
if j == -1 {
@ -115,7 +115,7 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
}
ms = issueFullPattern.FindAll(rawBytes, -1)
for _, m := range ms {
m = bytes.TrimPrefix(m, []byte(" "))
m = bytes.TrimSpace(m)
i := strings.Index(string(m), "issues/")
j := strings.Index(string(m), "#")
if j == -1 {
@ -126,9 +126,8 @@ func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
}
ms = issueIndexPattern.FindAll(rawBytes, -1)
for _, m := range ms {
m = bytes.TrimPrefix(m, []byte(" "))
rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
` <a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
`<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
}
return rawBytes
}

View file

@ -111,11 +111,11 @@ func SendResetPasswdMail(r *middleware.Render, user *models.User) {
SendAsync(&msg)
}
// SendNotifyMail sends mail notification of all watchers.
func SendNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) error {
// SendIssueNotifyMail sends mail notification of all watchers of repository.
func SendIssueNotifyMail(user, owner *models.User, repo *models.Repository, issue *models.Issue) ([]string, error) {
watches, err := models.GetWatches(repo.Id)
if err != nil {
return errors.New("mail.NotifyWatchers(get watches): " + err.Error())
return nil, errors.New("mail.NotifyWatchers(get watches): " + err.Error())
}
tos := make([]string, 0, len(watches))
@ -126,20 +126,37 @@ func SendNotifyMail(user, owner *models.User, repo *models.Repository, issue *mo
}
u, err := models.GetUserById(uid)
if err != nil {
return errors.New("mail.NotifyWatchers(get user): " + err.Error())
return nil, errors.New("mail.NotifyWatchers(get user): " + err.Error())
}
tos = append(tos, u.Email)
}
if len(tos) == 0 {
return nil
return tos, nil
}
subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name)
content := fmt.Sprintf("%s<br>-<br> <a href=\"%s%s/%s/issues/%d\">View it on Gogs</a>.",
issue.Content, base.AppUrl, owner.Name, repo.Name, issue.Index)
base.RenderSpecialLink([]byte(issue.Content), owner.Name+"/"+repo.Name),
base.AppUrl, owner.Name, repo.Name, issue.Index)
msg := NewMailMessageFrom(tos, user.Name, subject, content)
msg.Info = fmt.Sprintf("Subject: %s, send notify emails", subject)
msg.Info = fmt.Sprintf("Subject: %s, send issue notify emails", subject)
SendAsync(&msg)
return tos, nil
}
// SendIssueMentionMail sends mail notification for who are mentioned in issue.
func SendIssueMentionMail(user, owner *models.User, repo *models.Repository, issue *models.Issue, tos []string) error {
if len(tos) == 0 {
return nil
}
issueLink := fmt.Sprintf("%s%s/%s/issues/%d", base.AppUrl, owner.Name, repo.Name, issue.Index)
body := fmt.Sprintf(`%s mentioned you.`)
subject := fmt.Sprintf("[%s] %s", repo.Name, issue.Name)
content := fmt.Sprintf("%s<br>-<br> <a href=\"%s\">View it on Gogs</a>.", body, issueLink)
msg := NewMailMessageFrom(tos, user.Name, subject, content)
msg.Info = fmt.Sprintf("Subject: %s, send issue mention emails", subject)
SendAsync(&msg)
return nil
}

View file

@ -29,13 +29,13 @@ import (
"github.com/gogits/session"
"github.com/gogits/gogs/modules/log"
"github.com/gogits/gogs/modules/middleware"
)
const (
codeRedirect = 302
keyToken = "oauth2_token"
keyNextPage = "next"
keyToken = "oauth2_token"
keyNextPage = "next"
)
var (
@ -179,42 +179,49 @@ var LoginRequired martini.Handler = func() martini.Handler {
token := unmarshallToken(ctx.Session)
if token == nil || token.IsExpired() {
next := url.QueryEscape(ctx.Req.URL.RequestURI())
ctx.Redirect(PathLogin+"?next="+next, codeRedirect)
ctx.Redirect(PathLogin + "?next=" + next)
return
}
}
}()
func login(t *oauth.Transport, ctx *middleware.Context) {
next := extractPath(ctx.Req.URL.Query().Get(keyNextPage))
next := extractPath(ctx.Query(keyNextPage))
if ctx.Session.Get(keyToken) == nil {
// User is not logged in.
ctx.Redirect(t.Config.AuthCodeURL(next), codeRedirect)
ctx.Redirect(t.Config.AuthCodeURL(next))
return
}
// No need to login, redirect to the next page.
ctx.Redirect(next, codeRedirect)
ctx.Redirect(next)
}
func logout(t *oauth.Transport, ctx *middleware.Context) {
next := extractPath(ctx.Req.URL.Query().Get(keyNextPage))
next := extractPath(ctx.Query(keyNextPage))
ctx.Session.Delete(keyToken)
ctx.Redirect(next, codeRedirect)
ctx.Redirect(next)
}
func handleOAuth2Callback(t *oauth.Transport, ctx *middleware.Context) {
next := extractPath(ctx.Req.URL.Query().Get("state"))
code := ctx.Req.URL.Query().Get("code")
if errMsg := ctx.Query("error_description"); len(errMsg) > 0 {
log.Error("oauth2.handleOAuth2Callback: %s", errMsg)
return
}
next := extractPath(ctx.Query("state"))
code := ctx.Query("code")
tk, err := t.Exchange(code)
if err != nil {
// Pass the error message, or allow dev to provide its own
// error handler.
ctx.Redirect(PathError, codeRedirect)
log.Error("oauth2.handleOAuth2Callback(token.Exchange): %v", err)
// ctx.Redirect(PathError)
return
}
// Store the credentials in the session.
val, _ := json.Marshal(tk)
ctx.Session.Set(keyToken, val)
ctx.Redirect(next, codeRedirect)
ctx.Redirect(next)
}
func unmarshallToken(s session.SessionStore) (t *token) {