Skip to content

Commit

Permalink
Add Registration-Mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskaranbir committed Sep 27, 2018
1 parent 728dd6a commit a0f68c0
Show file tree
Hide file tree
Showing 13 changed files with 285 additions and 117 deletions.
4 changes: 1 addition & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
KAFKA_BROKERS=kafka:9092

KAFKA_CONSUMER_GROUP_LOGIN=auth.consumer.login
KAFKA_CONSUMER_TOPIC_LOGIN=auth.login.response
KAFKA_PRODUCER_TOPIC_LOGIN=auth.login.request

KAFKA_CONSUMER_GROUP_REGISTER=auth.consumer.register
KAFKA_CONSUMER_TOPIC_REGISTER=auth.register.response
KAFKA_PRODUCER_TOPIC_REGISTER=auth.register.request
KAFKA_PRODUCER_TOPIC_REGISTER=event.rns_eventstore.events

MONGO_HOSTS=localhost:27017
MONGO_USERNAME=root
Expand Down
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ===> Build Image
FROM golang:1.11.0-alpine3.8 AS builder
LABEL maintainer="Jaskaranbir Dhillon"

ARG SOURCE_REPO

ENV DEP_VERSION=0.5.0 \
CGO_ENABLED=0 \
GOOS=linux

# Download and install dep and git
ADD https://github.com/golang/dep/releases/download/v${DEP_VERSION}/dep-linux-amd64 /usr/bin/dep
RUN chmod +x /usr/bin/dep
RUN apk add --update git

WORKDIR $GOPATH/src/github.com/TerrexTech/${SOURCE_REPO}

# Copy the code from the host and compile it
COPY Gopkg.toml Gopkg.lock ./
RUN dep ensure --vendor-only -v
COPY . ./

RUN go build -v -a -installsuffix nocgo -o /app ./main

# ===> Run Image
FROM scratch
LABEL maintainer="Jaskaranbir Dhillon"

COPY --from=builder /app ./
ENTRYPOINT ["./app"]
24 changes: 10 additions & 14 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 0 additions & 39 deletions auth/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ package auth
import (
"github.com/TerrexTech/go-apigateway/model"
"github.com/TerrexTech/go-mongoutils/mongo"
"github.com/TerrexTech/uuuid"
"github.com/mongodb/mongo-go-driver/bson/objectid"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
)

// DBIConfig is the configuration for the authDB.
Expand All @@ -24,7 +21,6 @@ type DBIConfig struct {
// login, registeration etc.
type DBI interface {
Collection() *mongo.Collection
Register(user *model.User) (*model.User, error)
}

// DB is the implementation for dbI.
Expand Down Expand Up @@ -86,41 +82,6 @@ func EnsureAuthDB(dbConfig DBIConfig) (*DB, error) {
}, nil
}

// Register inserts the provided user into database
func (d *DB) Register(user *model.User) (*model.User, error) {
authUser := &model.User{
ID: objectid.New(),
FirstName: user.FirstName,
LastName: user.LastName,
Email: user.Email,
Role: user.Role,
Username: user.Username,
Password: user.Password,
}
uid, err := uuuid.NewV4()
if err != nil {
err = errors.Wrap(err, "Registration: Error generating UUID")
return nil, err
}
authUser.UUID = uid

hashedPass, err := bcrypt.GenerateFromPassword([]byte(user.Password), 10)
if err != nil {
err = errors.Wrap(err, "Registration: Error creating Hash for password")
return nil, err
}
authUser.Password = string(hashedPass)

_, err = d.collection.InsertOne(authUser)
if err != nil {
err = errors.Wrap(err, "Registration: Error inserting user into Database")
return nil, err
}
// Don't send hashed-password to any other service
authUser.Password = ""
return authUser, nil
}

// Collection returns the currrent MongoDB collection being used for user-auth operations.
func (d *DB) Collection() *mongo.Collection {
return d.collection
Expand Down
82 changes: 57 additions & 25 deletions gql/resolver/authhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package resolver
import (
"context"
"encoding/json"
"log"
"time"

"github.com/TerrexTech/go-apigateway/gwerrors"

"github.com/Shopify/sarama"
"github.com/TerrexTech/go-apigateway/auth"
"github.com/TerrexTech/go-apigateway/kafka"
Expand All @@ -17,7 +20,7 @@ import (
// authResponse is the GraphQL response on successful authentication.
type authResponse struct {
authResponse *model.AuthResponse
authErr error
authErr *gwerrors.KRError
}

// AccessTokenResolver is the resolver for AccessToken type.
Expand Down Expand Up @@ -52,7 +55,6 @@ var authHandler = func(
// Timeout Context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
authResChan := make(chan *authResponse)

authResponseLoop:
// Check auth-response messages for matching CorrelationID and return result
Expand All @@ -62,10 +64,13 @@ authResponseLoop:
break authResponseLoop
case msg := <-cio.ConsumerMessages():
cio.MarkOffset() <- msg
go handleAuthResponse(msg, ts, cid, authResChan)
authRes := <-authResChan
if authRes.authErr == nil {
return authRes.authResponse, nil
authRes := handleAuthResponse(msg, ts, cid)
if authRes != nil {
if authRes.authErr == nil {
return authRes.authResponse, nil
}
log.Println("ppppppppppppppppppppppp")
return nil, errors.New(authRes.authErr.Error())
}
}
}
Expand All @@ -77,39 +82,42 @@ func handleAuthResponse(
msg *sarama.ConsumerMessage,
ts auth.TokenStoreI,
cid uuuid.UUID,
outChan chan<- *authResponse,
) {
user, err := parseKafkaResponse(msg, cid)
if err != nil {
err = errors.Wrap(err, "Error authenticating user")
outChan <- &authResponse{
) *authResponse {
user, krerr := parseKafkaResponse(msg, cid)
if krerr != nil {
err := errors.Wrap(krerr, "Error authenticating user")
krerr.Err = err
return &authResponse{
authResponse: nil,
authErr: err,
authErr: krerr,
}
return
}

if user == nil {
return nil
}

at, err := genAccessToken(user)
if err != nil {
err = errors.Wrap(err, "Error generating AccessToken")
outChan <- &authResponse{
krerr := gwerrors.NewKRError(err, gwerrors.InternalError, err.Error())
return &authResponse{
authResponse: nil,
authErr: err,
authErr: krerr,
}
return
}

rt, err := genRefreshToken(ts, user)
if err != nil {
err = errors.Wrap(err, "Error generating RefreshToken")
outChan <- &authResponse{
krerr := gwerrors.NewKRError(err, gwerrors.InternalError, err.Error())
return &authResponse{
authResponse: nil,
authErr: err,
authErr: krerr,
}
return
}

outChan <- &authResponse{
return &authResponse{
authResponse: &model.AuthResponse{
AccessToken: at,
RefreshToken: rt,
Expand All @@ -118,25 +126,49 @@ func handleAuthResponse(
}
}

func parseKafkaResponse(msg *sarama.ConsumerMessage, cid uuuid.UUID) (*model.User, error) {
func parseKafkaResponse(
msg *sarama.ConsumerMessage, cid uuuid.UUID,
) (*model.User, *gwerrors.KRError) {
log.Println("aaaaaaaaaaaaaaaaaaaaaa")
kr := &esmodel.KafkaResponse{}
err := json.Unmarshal(msg.Value, kr)
if err != nil {
err = errors.Wrap(err, "LoginResponseHandler: Error unmarshalling message into KafkaResponse")
return nil, err
log.Println(err)
return nil, nil
}
log.Println("zzzzzzzzzzzzzzzzzzzz")
log.Printf("%+v", kr)
if kr.AggregateID != 1 {
return nil, nil
}

log.Println("bbbbbbbbbbbbbbbbbbbbbbb")
if cid.String() != kr.CorrelationID.String() {
return nil, errors.New("LoginResponseHandler: CorrelationID mistmatch")
log.Printf(
"Error: Correlation ID Mistmatch: Expected CorrelationID: %s, Got: %s",
cid.String(),
kr.CorrelationID.String(),
)
return nil, nil
}

if kr.Error != "" {
err = errors.Wrap(errors.New(kr.Error), "AuthKafkaResponseHandler Error")
krerr := gwerrors.NewKRError(err, kr.ErrorCode, err.Error())
return nil, krerr
}

log.Println("cccccccccccccccccccccccc")
user := &model.User{}
err = json.Unmarshal([]byte(kr.Result), user)
if err != nil {
err = errors.Wrap(err, "LoginResponseHandler: Error unmarshalling message-result into User")
return nil, err
krerr := gwerrors.NewKRError(err, gwerrors.InternalError, err.Error())
return nil, krerr
}

log.Println("dddddddddddddddddddddddd")
return user, nil
}

Expand Down
Loading

0 comments on commit a0f68c0

Please sign in to comment.