Skip to content

Commit

Permalink
oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
dakom committed May 16, 2018
1 parent 87380fe commit 2c0f74b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 36 deletions.
50 changes: 42 additions & 8 deletions endpoints/accounts/account-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,53 @@ package accounts
import (
"github.com/dakom/basic-site-api/lib/pages"

"encoding/json"
"github.com/dakom/basic-site-api/lib/datastore"
_ "image/gif"
_ "image/png"

"strconv"
)

type UserInfo struct {
Id string `json:"uid"`
Email string `json:"email"`
FirstName string `json:"fname"`
LastName string `json:"lname"`
DisplayName string `json:"dname"`
AvatarId string `json:"avid"`
Jwt string `json:"jwt"`
ErrorCode string `json:"code"`
}

func (u *UserInfo) GetString() (string, error) {
jBytes, err := json.Marshal(u)
if err != nil {
return "", err
}

return string(jBytes), nil
}

func (u *UserInfo) SetJwt(jwt string) {
u.Jwt = jwt
}
func (u *UserInfo) SetErrorCode(code string) {
u.ErrorCode = code
}

func GetUserInfoFromRecord(userRecord *datastore.UserRecord) *UserInfo {
return &UserInfo{
Id: userRecord.GetKeyIntAsString(),
Email: userRecord.GetData().Email,
FirstName: userRecord.GetData().FirstName,
LastName: userRecord.GetData().LastName,
DisplayName: userRecord.GetData().DisplayName,
AvatarId: strconv.FormatInt(userRecord.GetData().AvatarId, 10),
}
}

func GotSettingsInfoServiceRequest(rData *pages.RequestData) {
rData.SetJsonSuccessResponse(pages.JsonMapGeneric{
"uid": rData.UserRecord.GetKeyIntAsString(),
"email": rData.UserRecord.GetData().Email,
"fname": rData.UserRecord.GetData().FirstName,
"lname": rData.UserRecord.GetData().LastName,
"dname": rData.UserRecord.GetData().DisplayName,
"avid": strconv.FormatInt(rData.UserRecord.GetData().AvatarId, 10),
})
userInfo := GetUserInfoFromRecord(rData.UserRecord)
rData.SetJsonSuccessResponse(userInfo)
}
37 changes: 16 additions & 21 deletions endpoints/accounts/accounts-login.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package accounts

import (
"errors"
"strconv"
"strings"

"github.com/dakom/basic-site-api/lib/auth"
Expand All @@ -13,80 +12,76 @@ import (
)

func GotLoginServiceRequest(rData *pages.RequestData) {

//
username := strings.ToLower(strings.TrimSpace(rData.HttpRequest.FormValue("uname")))
password := rData.HttpRequest.FormValue("pw")
audience := strings.ToLower(strings.TrimSpace(rData.HttpRequest.FormValue("aud"))) //for web environments, we might want to allow setting this to cookie...

userRecord, _, jwtString, err := DoLogin(rData, username, password, audience, LOOKUP_TYPE_USERNAME)
userRecord, userInfo, _, jwtString, err := DoLogin(rData, username, password, audience, LOOKUP_TYPE_USERNAME)

if err != nil {

if userRecord == nil {

rData.SetJsonErrorCodeResponse(err.Error()) //nousername
} else {
userInfo := pages.JsonMapGeneric{
"uid": userRecord.GetKeyIntAsString(),
"fname": userRecord.GetData().FirstName,
"lname": userRecord.GetData().LastName,
"avid": strconv.FormatInt(userRecord.GetData().AvatarId, 10),
}

rData.SetJsonErrorCodeWithDataResponse(err.Error(), userInfo)
}

return
}

rData.SetJsonSuccessResponse(pages.JsonMapGeneric{"jwt": jwtString})
userInfo.SetJwt(jwtString)

rData.SetJsonSuccessResponse(userInfo)
}

func DoLogin(rData *pages.RequestData, username string, password string, audience string, lookupType int64) (*datastore.UserRecord, *datastore.JwtRecord, string, error) {
func DoLogin(rData *pages.RequestData, username string, password string, audience string, lookupType int64) (*datastore.UserRecord, *UserInfo, *datastore.JwtRecord, string, error) {

if len(username) < 1 {
return nil, nil, "", errors.New(statuscodes.MISSING_USERNAME)
return nil, nil, nil, "", errors.New(statuscodes.MISSING_USERNAME)
}

if lookupType != LOOKUP_TYPE_OAUTH && strings.HasPrefix(username, rData.SiteConfig.OAUTH_USERID_PREFIX) {
return nil, nil, "", errors.New(statuscodes.NOUSERNAME)
return nil, nil, nil, "", errors.New(statuscodes.NOUSERNAME)
}

userRecord, err := GetUserRecordViaUsername(rData.Ctx, username)
if err != nil {
rData.LogError(err.Error())
return nil, nil, "", errors.New(statuscodes.TECHNICAL)
return nil, nil, nil, "", errors.New(statuscodes.TECHNICAL)
}

if userRecord == nil {
return nil, nil, "", errors.New(statuscodes.NOUSERNAME)
return nil, nil, nil, "", errors.New(statuscodes.NOUSERNAME)

}

userInfo := GetUserInfoFromRecord(userRecord)

if !userRecord.GetData().IsActive {
return userRecord, nil, "", errors.New(statuscodes.NOT_ACTIVATED)
return userRecord, userInfo, nil, "", errors.New(statuscodes.NOT_ACTIVATED)
}

if lookupType != LOOKUP_TYPE_OAUTH {
if len(password) < 1 {
return userRecord, nil, "", errors.New(statuscodes.MISSING_PASSWORD)
return userRecord, userInfo, nil, "", errors.New(statuscodes.MISSING_PASSWORD)
}

if !cipher.ComparePWHash(password, userRecord.GetData().Password) {
return userRecord, nil, "", errors.New(statuscodes.WRONG_PASSWORD)
return userRecord, userInfo, nil, "", errors.New(statuscodes.WRONG_PASSWORD)
}
}

jwtRecord, jwtString, err := auth.GetNewLoginJWT(rData, userRecord, audience)

if err != nil {
return userRecord, nil, "", errors.New(statuscodes.TECHNICAL)
return userRecord, userInfo, nil, "", errors.New(statuscodes.TECHNICAL)
}

if audience == auth.JWT_AUDIENCE_COOKIE {
auth.SetJWTCookie(rData, jwtString, jwtRecord.GetData().SessionId, int(auth.GetFinalDurationByAudience(jwtRecord.GetData().Audience)))
}

return userRecord, jwtRecord, jwtString, nil
return userRecord, userInfo, jwtRecord, jwtString, nil
}
15 changes: 8 additions & 7 deletions endpoints/accounts/accounts-oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *StateInfo) ErrorUrl(statusCode string) string {
return s.Scheme + "status/" + statusCode
}

type UserInfo struct {
type OAuthUserInfo struct {
Id string `json:"uid,omitempty" datastore:",noindex"`
Email string `json:"email,omitempty" datastore:",noindex"`
FirstName string `json:"fname,omitempty" datastore:",noindex"`
Expand Down Expand Up @@ -246,7 +246,7 @@ func OauthAction(rData *pages.RequestData) {
}

if state.Request == "userinfo" {
var userInfo UserInfo
var userInfo OAuthUserInfo
if err := json.Unmarshal([]byte(state.Response), &userInfo); err != nil {
rData.SetJsonErrorCodeResponse(statuscodes.AUTH)
return
Expand All @@ -265,7 +265,7 @@ func OauthAction(rData *pages.RequestData) {
response["meta"] = requestMeta
}

_, _, jwtString, err := DoLogin(rData, userInfo.Id, "", requestMeta.Audience, LOOKUP_TYPE_OAUTH)
_, userRecordInfo, _, jwtString, err := DoLogin(rData, userInfo.Id, "", requestMeta.Audience, LOOKUP_TYPE_OAUTH)

if err != nil {
response["code"] = err.Error()
Expand All @@ -274,6 +274,7 @@ func OauthAction(rData *pages.RequestData) {
}

response["jwt"] = jwtString
response["userInfo"] = userRecordInfo
rData.SetJsonSuccessResponse(response)
return

Expand Down Expand Up @@ -359,7 +360,7 @@ func getStateAndRecordFromJwtString(rData *pages.RequestData, stateJwtString str
return stateJwtRecord, nil, errors.New(statuscodes.AUTH)
}

func getUserInfoFromRequest(rData *pages.RequestData, state *StateInfo, code string) *UserInfo {
func getUserInfoFromRequest(rData *pages.RequestData, state *StateInfo, code string) *OAuthUserInfo {

endpointConfig := getEndpointConfig(rData, state)
if endpointConfig == nil {
Expand All @@ -375,12 +376,12 @@ func getUserInfoFromRequest(rData *pages.RequestData, state *StateInfo, code str

client := endpointConfig.Client(rData.Ctx, tok)

var userInfo *UserInfo
var userInfo *OAuthUserInfo

if state.Provider == "google" {
googleUserInfo, err := getInfo_Google(rData, client)
if err == nil {
userInfo = &UserInfo{
userInfo = &OAuthUserInfo{
Id: googleUserInfo.Id,
Email: googleUserInfo.Email,
FirstName: googleUserInfo.GivenName,
Expand All @@ -391,7 +392,7 @@ func getUserInfoFromRequest(rData *pages.RequestData, state *StateInfo, code str
} else if state.Provider == "facebook" {
facebookUserInfo, err := getInfo_Facebook(rData, client, tok.AccessToken)
if err == nil {
userInfo = &UserInfo{
userInfo = &OAuthUserInfo{
Id: facebookUserInfo.Id,
Email: facebookUserInfo.Email,
FirstName: facebookUserInfo.FirstName,
Expand Down

0 comments on commit 2c0f74b

Please sign in to comment.