2017-05-06 21:21:39 +02:00
package userService
import (
"errors"
2017-05-10 00:04:07 +02:00
"fmt"
2017-05-06 21:21:39 +02:00
"net/http"
2017-05-10 00:04:07 +02:00
"strconv"
"time"
2017-05-06 21:21:39 +02:00
"github.com/ewhal/nyaa/config"
"github.com/ewhal/nyaa/db"
"github.com/ewhal/nyaa/model"
"github.com/ewhal/nyaa/util/email"
2017-05-10 00:04:07 +02:00
"github.com/ewhal/nyaa/util/timeHelper"
"github.com/gorilla/securecookie"
2017-05-06 21:21:39 +02:00
"github.com/nicksnyder/go-i18n/i18n"
)
2017-05-10 00:04:07 +02:00
var verificationHandler = securecookie . New ( config . EmailTokenHashKey , nil )
2017-05-06 21:21:39 +02:00
// SendEmailVerfication sends an email verification token via email.
2017-05-10 00:04:07 +02:00
func SendEmailVerification ( to string , token string , locale string ) error {
2017-05-06 21:21:39 +02:00
T , _ := i18n . Tfunc ( locale )
2017-05-10 00:04:07 +02:00
content := T ( "link" ) + " : https://" + config . WebAddress + "/verify/email/" + token
content_html := T ( "verify_email_content" ) + "<br/>" + "<a href=\"https://" + config . WebAddress + "/verify/email/" + token + "\" target=\"_blank\">" + config . WebAddress + "/verify/email/" + token + "</a>"
return email . SendEmailFromAdmin ( to , T ( "verify_email_title" ) , content , content_html )
return nil
2017-05-06 21:21:39 +02:00
}
// SendVerificationToUser sends an email verification token to user.
func SendVerificationToUser ( user model . User ) ( int , error ) {
2017-05-10 00:04:07 +02:00
validUntil := timeHelper . TwentyFourHoursLater ( ) // TODO: longer duration?
value := map [ string ] string {
"t" : strconv . FormatInt ( validUntil . Unix ( ) , 10 ) ,
"u" : strconv . FormatUint ( uint64 ( user . Id ) , 10 ) ,
"e" : user . Email ,
2017-05-06 21:21:39 +02:00
}
2017-05-10 00:04:07 +02:00
encoded , err := verificationHandler . Encode ( "" , value )
2017-05-06 21:21:39 +02:00
if err != nil {
2017-05-10 00:04:07 +02:00
return http . StatusInternalServerError , err
2017-05-06 21:21:39 +02:00
}
2017-05-10 00:04:07 +02:00
err = SendEmailVerification ( user . Email , encoded , "en-us" )
2017-05-06 21:21:39 +02:00
if err != nil {
return http . StatusInternalServerError , err
}
2017-05-10 00:04:07 +02:00
return http . StatusOK , nil
2017-05-06 21:21:39 +02:00
}
// SendVerification sends an email verification token.
func SendVerification ( r * http . Request ) ( int , error ) {
var user model . User
currentUser , err := CurrentUser ( r )
if err != nil {
return http . StatusUnauthorized , errors . New ( "Unauthorized." )
}
if db . ORM . First ( & user , currentUser . Id ) . RecordNotFound ( ) {
return http . StatusNotFound , errors . New ( "User is not found." )
}
status , err := SendVerificationToUser ( user )
return status , err
}
2017-05-10 00:04:07 +02:00
// EmailVerification verifies the token used for email verification
func EmailVerification ( token string , w http . ResponseWriter ) ( int , error ) {
value := make ( map [ string ] string )
err := verificationHandler . Decode ( "" , token , & value )
if err != nil {
fmt . Printf ( "%+v\n" , err )
return http . StatusForbidden , errors . New ( "Token is not valid." )
2017-05-06 21:21:39 +02:00
}
2017-05-10 00:04:07 +02:00
time_int , _ := strconv . ParseInt ( value [ "t" ] , 10 , 0 )
if timeHelper . IsExpired ( time . Unix ( time_int , 0 ) ) {
return http . StatusForbidden , errors . New ( "Token has expired." )
2017-05-06 21:21:39 +02:00
}
2017-05-10 00:04:07 +02:00
var user model . User
if db . ORM . Where ( "user_id = ?" , value [ "u" ] ) . First ( & user ) . RecordNotFound ( ) {
return http . StatusNotFound , errors . New ( "User is not found." )
2017-05-06 21:21:39 +02:00
}
2017-05-10 00:04:07 +02:00
user . Email = value [ "e" ]
return UpdateUserCore ( & user )
2017-05-06 21:21:39 +02:00
}