Skip to content

Commit

Permalink
fix: don't allow session.connected to be set unless authenticated
Browse files Browse the repository at this point in the history
  • Loading branch information
mvayngrib committed Nov 26, 2017
1 parent 41fba12 commit bc98c52
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 38 deletions.
17 changes: 13 additions & 4 deletions lib/auth.js

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

21 changes: 15 additions & 6 deletions lib/delivery-mqtt.js

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

5 changes: 3 additions & 2 deletions lib/logger.js

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

1 change: 0 additions & 1 deletion lib/routes/auth.js

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

1 change: 0 additions & 1 deletion lib/routes/preauth.js

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

18 changes: 15 additions & 3 deletions lib/user.js

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

18 changes: 15 additions & 3 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,21 @@ export default class Auth {
connected: boolean
}): Promise<any> => {
const { clientId, connected } = opts
const params: any = getUpdateParams({ connected })
params.Key = getKeyFromClientId(clientId)
return this.tables.Presence.update(params)
// const params:any = getUpdateParams({ connected })
// params.Key = getKeyFromClientId(clientId)
return this.tables.Presence.update({
Key: getKeyFromClientId(clientId),
UpdateExpression: 'SET #connected = :connected',
ConditionExpression: '#authenticated = :authenticated',
ExpressionAttributeNames: {
'#connected': 'connected',
'#authenticated': 'authenticated'
},
ExpressionAttributeValues: {
':connected': true,
':authenticated': true
}
})
}

public deleteSession = (clientId: string): Promise<any> => {
Expand Down
22 changes: 16 additions & 6 deletions src/delivery-mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ proto.deliverBatch = co(function* ({ clientId, recipient, messages }) {
const strings = messages.map(stringify)
const subBatches = batchStringsBySize(strings, MAX_PAYLOAD_SIZE)
for (let subBatch of subBatches) {
yield this.iot.publish({
topic: this._prefixTopic(`${clientId}/sub/inbox`),
yield this.emit({
clientId,
topic: 'inbox',
payload: `{"messages":[${subBatch.join(',')}]}`
})
}
Expand All @@ -65,8 +66,9 @@ proto.deliverBatch = co(function* ({ clientId, recipient, messages }) {
proto.ack = function ack ({ clientId, message }) {
debug(`acking message from ${clientId}`)
const stub = this.messages.getMessageStub({ message })
return this.iot.publish({
topic: this._prefixTopic(`${clientId}/sub/ack`),
return this.emit({
clientId,
topic: 'ack',
payload: {
message: stub
}
Expand All @@ -76,15 +78,23 @@ proto.ack = function ack ({ clientId, message }) {
proto.reject = function reject ({ clientId, message, error }) {
debug(`rejecting message from ${clientId}`, error)
const stub = this.messages.getMessageStub({ message, error })
return this.iot.publish({
topic: this._prefixTopic(`${clientId}/sub/reject`),
return this.emit({
clientId,
topic: 'reject',
payload: {
message: stub,
reason: Errors.export(error)
}
})
}

proto.emit = function emit ({ clientId, topic, payload }) {
return this.iot.publish({
topic: this._prefixTopic(`${clientId}/sub/${topic}`),
payload
})
}

function stringify (msg) {
return JSON.stringify(omitVirtual(msg))
}
5 changes: 2 additions & 3 deletions src/delivery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ function withTransport (method: string) {
export default class Delivery extends EventEmitter implements IDelivery {
public ack = withTransport('ack')
public reject = withTransport('reject')

private mqtt: any
private http: DeliveryHTTP
public mqtt: any
public http: DeliveryHTTP
private friends: any
private messages: Messages
private objects: any
Expand Down
6 changes: 4 additions & 2 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// inspired by
// http://theburningmonk.com/2017/09/capture-and-forward-correlation-ids-through-different-lambda-event-sources/

import stringifySafe = require('json-stringify-safe')

export const Level = {
ERROR: 0,
WARN: 1,
Expand Down Expand Up @@ -152,10 +154,10 @@ export default class Logger {

if (params) logMsg.params = params

return JSON.stringify(logMsg)
return stringifySafe(logMsg)
}

const stringifiedParams = params ? JSON.stringify(params) : ''
const stringifiedParams = params ? stringifySafe(params) : ''
let part1 = this.namespace
if (part1) part1 += ':'

Expand Down
2 changes: 0 additions & 2 deletions src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ export = function attachHandler ({ tradle, router }: {
router.use(bodyParser.json({ limit: '10mb' }))
// router.use(bodyParser.urlencoded({ limit: '10mb', extended: true }))
router.post('/auth', coexpress(function* (req, res) {
yield init.ensureInitialized()

// debug('[START] /auth', Date.now())
const event = req.body
// TODO: use @tradle/validate-resource
Expand Down
2 changes: 0 additions & 2 deletions src/routes/preauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export = function attachHandler ({ tradle, router }: {
router.use(bodyParser.json({ limit: '10mb' }))
// router.use(bodyParser.urlencoded({ limit: '10mb', extended: true }))
router.post('/preauth', coexpress(function* (req, res) {
yield init.ensureInitialized()

// debug('[START]', now)
const ips = getRequestIps(req)
const { clientId, identity } = req.body
Expand Down
17 changes: 14 additions & 3 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,20 @@ proto.onDisconnected = function ({ clientId }) {
return this.auth.updatePresence({ clientId, connected: false })
}

proto.onConnected = function ({ clientId }) {
return this.auth.updatePresence({ clientId, connected: true })
}
proto.onConnected = co(function* ({ clientId }) {
try {
yield this.auth.updatePresence({ clientId, connected: true })
} catch (err) {
this.logger.error('failed to update presence information', err)
yield this.delivery.mqtt.emit({
clientId,
topic: 'error',
payload: {
message: 'please reconnect'
}
})
}
})

proto.onPreAuth = function (...args) {
return this.auth.createTemporaryIdentity(...args)
Expand Down

0 comments on commit bc98c52

Please sign in to comment.