Skip to content

Commit

Permalink
feat(repo): Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
herrmannplatz committed Jun 12, 2018
0 parents commit 0582bed
Show file tree
Hide file tree
Showing 11 changed files with 9,575 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
charset = utf-8
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
58 changes: 58 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: node_js
node_js:
- 8

cache:
directories:
- "node_modules"

before_script:
- npm prune

after_success:
- npm run semantic-release

branches:
only:
- master
21 changes: 21 additions & 0 deletions LICENCE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 herrmannplatz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# mapkit-token

[![npm version](https://badge.fury.io/js/mapkit-token.svg)](https://badge.fury.io/js/mapkit-token) [![Build Status](https://travis-ci.org/herrmannplatz/mapkit-token.svg?branch=master)](https://travis-ci.org/herrmannplatz/mapkit-token)

🗺 Easily generate MapKit authorization tokens.

## Usage
```javascript
const generate = reqire('mapkit-token')

const token = generate('AUTH_KEY', 'KEY_ID', 'TEAM_ID', 1 * 60, 'com.domain.my')
```

### `generate(authKey, keyId, teamId[, ttl=30*60, origin=undefined])`

* **authKey**: MapKit Authorization Key
* **keyId**: MapKit JS Key ID.
* **teamId**: Apple Developer Team ID.
* **ttl**: Time to live in seconds. Defaults to 30 minutes.
* **origin**: Domain restrictions. Optional.
18 changes: 18 additions & 0 deletions __snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`generate should generate a token 1`] = `
Object {
"exp": 3600,
"iat": 0,
"iss": "maps.org.team.id",
"origin": "org.team.id",
}
`;

exports[`generate should generate a token without ttl and origin 1`] = `
Object {
"exp": 1800,
"iat": 0,
"iss": "maps.org.team.id",
}
`;
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const jwt = require('jsonwebtoken')

function generate (authKey, keyId, teamId, ttl = 30 * 60, origin) {
if (authKey == null) {
throw new Error('Missing your MapKit Authorization Key')
}

if (keyId == null) {
throw new Error('Missing your MapKit JS Key ID')
}

if (teamId == null) {
throw new Error('Missing your Apple Developer Team ID')
}

const payload = {
iss: teamId,
iat: Date.now() / 1000,
exp: (Date.now() / 1000) + ttl,
origin
}

const header = {
kid: keyId,
typ: 'JWT',
alg: 'ES256'
}

return jwt.sign(payload, authKey, { header })
}

module.exports = generate
40 changes: 40 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-env jest */
const fs = require('fs')
const jwt = require('jsonwebtoken')
const generate = require('./')

// mock date
Date.now = jest.genMockFunction().mockReturnValue(0)

describe('generate', () => {
it('should generate a token', () => {
const authKey = fs.readFileSync('./key.p8')
const teamId = 'maps.org.team.id'
const keyId = 'c0c0c0'
const ttl = 60 * 60
const origin = 'org.team.id'

const token = generate(authKey, keyId, teamId, ttl, origin)
expect(jwt.decode(token)).toMatchSnapshot()
})

it('should generate a token without ttl and origin', () => {
const authKey = fs.readFileSync('./key.p8')
const teamId = 'maps.org.team.id'
const keyId = 'c0c0c0'

const token = generate(authKey, keyId, teamId)
expect(jwt.decode(token)).toMatchSnapshot()
})

it('should throw in case of missing parameters', () => {
const authKey = fs.readFileSync('./key.p8')
const teamId = 'maps.org.team.id'
const keyId = 'c0c0c0'

expect(() => generate()).toThrowError()
expect(() => generate(authKey)).toThrowError()
expect(() => generate(authKey, keyId)).toThrowError()
expect(() => generate(authKey, keyId, teamId)).not.toThrowError()
})
})
6 changes: 6 additions & 0 deletions key.p8
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQg90l9CyELs4Hk2sXx
dA6bY6TwNds8eQ6n7hJzIUPnRpygCgYIKoZIzj0DAQehRANCAATHmerdSOxytFt2
aF4jXy1kUDCmgbC8+8aQr4fxDc9c7L/BaRgMx277tBgHsgUV4dUiEWvPIrtjO1bv
pLwPH8Tt
-----END PRIVATE KEY-----
Loading

0 comments on commit 0582bed

Please sign in to comment.