Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f49849

Browse files
committedApr 9, 2015
Initial. API with JWT Authentication
0 parents  commit 7f49849

36 files changed

+2344
-0
lines changed
 

‎.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Created by https://www.gitignore.io
2+
3+
### Go ###
4+
# Compiled Object files, Static and Dynamic libs (Shared Objects)
5+
*.o
6+
*.a
7+
*.so
8+
9+
# Folders
10+
_obj
11+
_test
12+
13+
# Architecture specific extensions/prefixes
14+
*.[568vq]
15+
[568vq].out
16+
17+
*.cgo1.go
18+
*.cgo2.c
19+
_cgo_defun.c
20+
_cgo_gotypes.go
21+
_cgo_export.*
22+
23+
_testmain.go
24+
25+
*.exe
26+
*.test
27+
*.prof
28+
.vagrant

‎Vagrantfile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- mode: ruby -*-
2+
# vi: set ft=ruby :
3+
4+
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
5+
VAGRANTFILE_API_VERSION = "2"
6+
7+
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
8+
# Every Vagrant virtual environment requires a box to build off of.
9+
config.vm.box = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"
10+
config.vm.synced_folder '.', '/home/vagrant/go/src/api.jwt.auth/'
11+
12+
13+
# Create a forwarded port mapping which allows access to a specific port
14+
# within the machine from a port on the host machine. In the example below,
15+
# accessing "localhost:8080" will access port 80 on the guest machine.
16+
#config.vm.network "forwarded_port", guest: 8000, host: 8080
17+
18+
# Create a public network, which generally matched to bridged network.
19+
# Bridged networks make the machine appear as another physical device on
20+
# your network.
21+
config.vm.network "public_network", ip: "192.168.1.210"
22+
23+
config.vm.provision "ansible" do |ansible|
24+
ansible.playbook = "provision/playbook.yml"
25+
ansible.host_key_checking = false
26+
ansible.verbose = "vvvv"
27+
end
28+
29+
end

‎api/parameters/auth.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package parameters
2+
3+
import ()
4+
5+
type TokenAuthentication struct {
6+
Token string `json:"token" form:"token"`
7+
}

‎controllers/auth_controller.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package controllers
2+
3+
import (
4+
"api.jwt.auth/api/parameters"
5+
"api.jwt.auth/core/authentication"
6+
"api.jwt.auth/services/models"
7+
"encoding/json"
8+
"net/http"
9+
)
10+
11+
func Login(w http.ResponseWriter, r *http.Request) {
12+
request_user := new(models.User)
13+
decoder := json.NewDecoder(r.Body)
14+
decoder.Decode(&request_user)
15+
16+
authBackend := authentication.InitJWTAuthenticationBackend()
17+
18+
if authBackend.Authenticate(request_user) {
19+
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
20+
response, _ := json.Marshal(token)
21+
w.Header().Set("Content-Type", "application/json")
22+
w.Write(response)
23+
24+
} else {
25+
w.Header().Set("Content-Type", "application/json")
26+
w.WriteHeader(http.StatusUnauthorized)
27+
w.Write([]byte("Unauthorized"))
28+
}
29+
}
30+
31+
func RefresfhToken(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
32+
authBackend := authentication.InitJWTAuthenticationBackend()
33+
token := parameters.TokenAuthentication{authBackend.GenerateToken()}
34+
response, _ := json.Marshal(token)
35+
w.Header().Set("Content-Type", "application/json")
36+
w.Write(response)
37+
}
38+
39+
func Logout(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
40+
w.Write([]byte("Unauthorized"))
41+
}

‎controllers/hello_controller.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package controllers
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func HelloController(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
8+
w.Write([]byte("Hello, World!"))
9+
}

‎core/authentication/jwt_backend.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package authentication
2+
3+
import (
4+
"api.jwt.auth/services/models"
5+
jwt "github.com/dgrijalva/jwt-go"
6+
"golang.org/x/crypto/bcrypt"
7+
"io/ioutil"
8+
"path/filepath"
9+
)
10+
11+
type JWTAuthenticationBackend struct {
12+
privateKey []byte
13+
PublicKey []byte
14+
}
15+
16+
func InitJWTAuthenticationBackend() *JWTAuthenticationBackend {
17+
authBack := new(JWTAuthenticationBackend)
18+
privateKeyPath, _ := filepath.Abs("./core/authentication/keys/private_key")
19+
publicKeyPath, _ := filepath.Abs("./core/authentication/keys/public_key.pub")
20+
authBack.privateKey, _ = ioutil.ReadFile(privateKeyPath)
21+
authBack.PublicKey, _ = ioutil.ReadFile(publicKeyPath)
22+
23+
return authBack
24+
}
25+
26+
func (backend *JWTAuthenticationBackend) GenerateToken() string {
27+
token := jwt.New(jwt.GetSigningMethod("RS256"))
28+
tokenString, _ := token.SignedString(backend.privateKey)
29+
return tokenString
30+
}
31+
32+
func (backend *JWTAuthenticationBackend) Authenticate(user *models.User) bool {
33+
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("testing"), 10)
34+
35+
testUser := models.User{
36+
Username: "haku",
37+
Password: string(hashedPassword),
38+
}
39+
40+
return user.Username == testUser.Username && bcrypt.CompareHashAndPassword([]byte(testUser.Password), []byte(user.Password)) == nil
41+
}
42+
43+
func (backend *JWTAuthenticationBackend) Logout(token string) error {
44+
return nil
45+
}

‎core/authentication/keys/private_key

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIEowIBAAKCAQEA4w5xhil8YFSLptRxzQsiJgQm7DxfVx7nEFAndQDw/7a1VfIf
3+
hhzZlUYx6u+57kP4+JPhqLMl9hEPnJh2DMPV4wrQAOSe6pDK5UP/xZQx8ygy70lG
4+
fJ6MVo7mkXKaofKobOhkFIOhqtLU/6CrzFl+KdFIsD7pt+FxV6mMmPbnAvDN+hF5
5+
NwU6N61WGAZER8z7SSTgayGpuHdUKCdPwfuiUIEX3GxhskzV/ROiS+R/NbQZlsfm
6+
QqcBJ5FxhOtAVevi9s7x6LLTSQKopuuunSTTtu3ys/hs5m6AqNPPkLKqp6R8iXF1
7+
Lg0DMeQlFHYwEo3oRweMNhfYRzC3ukioSf+GuwIDAQABAoIBADlemeKLMujoE80Y
8+
WpSzXnJ6lBcWfgR2Q23EwuN2VG5YDONlZP+u5G8qKEyzO6hvNkYgn2DPuyS8VNR9
9+
VT6OcMmIHtxK57he01UwZDzY3/IPUydQvWWZbd4lBy7y5Q1MUbAK29avF7cgxD6+
10+
qwncBtusDJCzpLwYU1oR9ftkTyRXl8WzHUQ+/QILNnSCDsTrP8JsVaVxbd6FhKKn
11+
5sSyqM+dX7mtvVAOcj0OJSHZiit7fk5QG9Pi/5iP4pCdZf42sImsr++2GFOezfJd
12+
H5UU+ujTf+b4oGirnqgEDRrSr5IyykagWc07D2KJgyPzrkfFDxoB5C/ZC3C6C9AA
13+
Xwzd+GECgYEA5SPDfCMVBRFkYBoxKgbWEElquGiPMDSe+p6QSlX24UXFv8gzdtbT
14+
f33d27v2cpIOWYym3Er5JiSFq6oCr1cg9+mLP/tNc50sHrdHb8vRfn190nawFJHa
15+
eOe0b3ZePUtAxdd1HaZgq4bNnLYSbi//spdHuu6E1jZrzcmbvIm7PJECgYEA/awp
16+
rILMDvqHuGNlVr+kdcGfmFxA8y9Z1tZHLgqNjPQQlaOuyJn1cfYbIqghMLjk//Au
17+
VQ5gfKLc2abHQaVQ2dLqV846eNQvr+cnLQUrUqk41IZuN0HTMbvLHgOLkQNdsUMs
18+
1TmmPeMxh9X9cLqp7mZoY5CeWeWFOe3EJA1dZIsCgYEAklbf3yUMpJrx7wprQbrx
19+
9Z7dwH5OjGve6JJh9oemT0LfQ1dZvtj+ZBr/mPkXMR6keX6Bhol/S2Ph1ruSUWck
20+
0A/gdfFKCr9jUQ6eWgDif5UnyUUxuUFZNQRN0S3Yi+7GpFOxIUmDzagfIqmJZcPT
21+
2rwQ/IqeXayN9vR+ONABu3ECgYAECn4PdXXytyL6WPsASsU/6vmz36RZO2Pe/ELe
22+
BOUEXc7100mxgGJckmMURkFhGVDsktLqH/SBh8ak4PdDoHKNRcLd6zcbPaYU00XY
23+
fcCW7IMvP4T59F586FTwAXZztO4FKODJ9MUlLz1WwJ3s8cxLM+5tx5v+Kp3YsmTx
24+
fhUCyQKBgDCEkFexrqC2a1rHLh+pwTyvnE4JCVNt72FF8L51aEsG5tGGFvTvgUN6
25+
IlRCYASNhUK/3+hu337uOSolKXu0W+dFnp1/OLo6sUkuhxWGx3YLwGJygjSrOl5f
26+
3wIikQ0U/RjRr+/pI0/yw/w3Xcr7iUjei6SBxkiIeZL/749EcLNB
27+
-----END RSA PRIVATE KEY-----
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4w5xhil8YFSLptRxzQsi
3+
JgQm7DxfVx7nEFAndQDw/7a1VfIfhhzZlUYx6u+57kP4+JPhqLMl9hEPnJh2DMPV
4+
4wrQAOSe6pDK5UP/xZQx8ygy70lGfJ6MVo7mkXKaofKobOhkFIOhqtLU/6CrzFl+
5+
KdFIsD7pt+FxV6mMmPbnAvDN+hF5NwU6N61WGAZER8z7SSTgayGpuHdUKCdPwfui
6+
UIEX3GxhskzV/ROiS+R/NbQZlsfmQqcBJ5FxhOtAVevi9s7x6LLTSQKopuuunSTT
7+
tu3ys/hs5m6AqNPPkLKqp6R8iXF1Lg0DMeQlFHYwEo3oRweMNhfYRzC3ukioSf+G
8+
uwIDAQAB
9+
-----END PUBLIC KEY-----

‎core/authentication/middlewares.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package authentication
2+
3+
import (
4+
jwt "github.com/dgrijalva/jwt-go"
5+
"net/http"
6+
)
7+
8+
func RequireTokenAuthentication(rw http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
9+
authBackend := InitJWTAuthenticationBackend()
10+
11+
token, err := jwt.ParseFromRequest(req, func(token *jwt.Token) (interface{}, error) {
12+
return authBackend.PublicKey, nil
13+
})
14+
15+
if err == nil && token.Valid {
16+
next(rw, req)
17+
} else {
18+
rw.WriteHeader(http.StatusUnauthorized)
19+
}
20+
}

‎provision/playbook.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
- name: Create a {{ application_name }} virtual machine via vagrant
2+
hosts: all
3+
sudo: yes
4+
sudo_user: root
5+
remote_user: vagrant
6+
vars:
7+
- update_apt_cache: yes
8+
vars_files:
9+
- vars.yml
10+
11+
roles:
12+
- base
13+
- postgresql
14+
- golang
15+
- redis

‎provision/roles/base/tasks/main.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
- name: Ensure OpenSSl and libssl are the latest versions
2+
apt: name={{ item }} update_cache=yes state=latest
3+
with_items:
4+
- openssl
5+
- libssl-dev
6+
- libssl-doc
7+
tags: packages
8+
9+
- name: Install base packages
10+
apt: name={{ item }} update_cache=yes force=yes state=installed
11+
with_items:
12+
- build-essential
13+
- ntp
14+
- htop
15+
- git
16+
- meld
17+
- mercurial
18+
tags: packages
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export PATH=$PATH:/usr/local/go/bin
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export GOPATH=$HOME/go
2+
export PATH=$GOPATH/bin:$PATH

‎provision/roles/golang/tasks/main.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
- name: Download the Go tarball
3+
get_url: url={{ go_download_location }}
4+
dest=/usr/local/src/{{ go_tarball }}
5+
sha256sum={{ go_tarball_checksum }}
6+
7+
- name: Register the current Go version (if any)
8+
command: /usr/local/go/bin/go version
9+
ignore_errors: yes
10+
register: go_version
11+
12+
- name: Extract the Go tarball if Go is not yet installed or if it is not the desired version
13+
command: tar -C /usr/local -xf /usr/local/src/{{ go_tarball }}
14+
when: go_version|failed or go_version.stdout != go_version_target
15+
16+
- name: Add the Go bin directory to the PATH environment variable for all users
17+
copy: src=go-bin.sh
18+
dest=/etc/profile.d
19+
20+
- name: Set GOPATH for all users
21+
copy: src=go-path.sh
22+
dest=/etc/profile.d
23+

‎provision/roles/golang/vars/main.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
go_tarball: "go1.4.2.linux-amd64.tar.gz"
2+
go_tarball_checksum: "141b8345932641483c2437bdbd65488a269282ac85f91170805c273f03dd223b"
3+
go_version_target: "go version go1.4.2 linux/amd64"
4+
5+
go_download_location: "http://golang.org/dl/{{ go_tarball }}"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
# handlers file for postgresql
3+
- name: restart postgresql
4+
service:
5+
name: postgresql
6+
state: restarted
7+
arguments: "{{ pg_version }}"
8+
sudo: true
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: Install development headers
3+
apt: pkg="libpq-dev"
4+
sudo: yes
5+
when: pg_dev_headers == True
6+
tags:
7+
- postgresql
8+
9+
- name: Install PostgreSQL contribs
10+
apt: pkg="postgresql-contrib-{{ pg_version }}"
11+
sudo: yes
12+
when: pg_contrib
13+
tags:
14+
- postgresql
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
---
2+
- name: Ensure common packages are installed
3+
apt: pkg={{ item }} state=installed update_cache=yes cache_valid_time=3600
4+
with_items:
5+
- python-pycurl
6+
sudo: true
7+
tags: postgres_common
8+
9+
- name: Ensure packages are installed
10+
apt: pkg={{ item }}
11+
with_items:
12+
- postgresql-{{ pg_version }}
13+
- postgresql-client-{{ pg_version }}
14+
- python-psycopg2
15+
register: db_setup
16+
environment: pg_proxy_env
17+
sudo: true
18+
tags: postgres_packages
19+
20+
- name: Recreate cluster
21+
include: recreate_cluster.yml
22+
when: pg_cluster_recreate
23+
tags: postgres_cluster
24+
25+
- name: Update pg_hba.conf file
26+
template: src=pg_hba.conf.j2 dest=/etc/postgresql/{{ pg_version }}/{{ pg_cluster }}/pg_hba.conf owner={{ pg_admin_user }} group={{ pg_admin_user }} mode=0640
27+
notify: restart postgresql
28+
sudo: true
29+
tags: postgres_config
30+
31+
- name: Update postgres.conf file
32+
template: src=master.conf.j2 dest=/etc/postgresql/{{ pg_version }}/{{ pg_cluster }}/postgresql.conf owner={{ pg_admin_user }} group={{ pg_admin_user }} mode=0644
33+
sudo: true
34+
notify: restart postgresql
35+
tags: postgres_config
36+
37+
- include: extensions_common.yml
38+
tags: postgres_extensions
39+
40+
- meta: flush_handlers
41+
42+
- name: ensure postgresql server is started
43+
service:
44+
name: postgresql
45+
state: started
46+
enabled: yes
47+
arguments: "{{ pg_version }}"
48+
pattern: "/usr/lib/postgresql/{{ pg_version | float }}/bin/postgres -D /var/lib/postgresql/{{ pg_version }}/{{ pg_cluster }}"
49+
sudo: true
50+
tags: postgres_start
51+
52+
- include: postgis.yml
53+
when: pg_postgis
54+
tags: postgres_postgis
55+
56+
- name: Ensure database is created
57+
sudo_user: root
58+
sudo: yes
59+
postgresql_db: name={{ db_name }}
60+
template='template0'
61+
state=present
62+
login_user='postgres'
63+
login_password='postgres'
64+
65+
- name: Ensure user has access to the database
66+
sudo_user: root
67+
sudo: yes
68+
postgresql_user: db={{ db_name }}
69+
name={{ db_user }}
70+
password={{ db_password }}
71+
priv=ALL
72+
state=present
73+
login_user='postgres'
74+
login_password='postgres'
75+
76+
77+
- name: Ensure user does not have unnecessary privileges
78+
sudo_user: root
79+
sudo: yes
80+
postgresql_user: name={{ db_user }}
81+
role_attr_flags=NOSUPERUSER,NOCREATEDB
82+
state=present
83+
login_user='postgres'
84+
login_password='postgres'
85+
86+
- name: postgresql - create db
87+
sudo_user: root
88+
sudo: yes
89+
postgresql_db: name={{ db_name }}
90+
encoding="UTF-8"
91+
template='template0'
92+
login_user='postgres'
93+
login_password='postgres'
94+
95+
- name: Ensure database is created
96+
sudo_user: root
97+
sudo: yes
98+
postgresql_db: name={{ db_name }}
99+
template='template0'
100+
state=present
101+
login_user='postgres'
102+
login_password='postgres'
103+
104+
- name: postgresql - create db Tests
105+
sudo_user: root
106+
sudo: yes
107+
postgresql_db: name={{ db_name_tests }}
108+
encoding="UTF-8"
109+
template='template0'
110+
login_user='postgres'
111+
login_password='postgres'
112+
113+
- name: Ensure database is created Tests
114+
sudo_user: root
115+
sudo: yes
116+
postgresql_db: name={{ db_name_tests }}
117+
template='template0'
118+
state=present
119+
login_user='postgres'
120+
login_password='postgres'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
- name: Install postgis extensions
3+
sudo: yes
4+
apt: pkg={{ item }}
5+
with_items:
6+
- "postgresql-{{ pg_version }}-postgis-{{ pg_postgis_version }}"
7+
- libgeos-c1
8+
tags:
9+
- postgresql
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
- name: Setup postgres cluster to default to utf8 | stop cluster
3+
# if the dbs haven't been created yet, we want to destroy the whole db
4+
# cluster and recreate it with proper utf8 support.
5+
sudo: yes
6+
sudo_user: postgres
7+
shell: pg_dropcluster --stop {{ pg_version }} {{ pg_cluster }}
8+
tags:
9+
- postgres
10+
11+
- name: Setup postgres cluster to default to utf8 | start cluster
12+
# if the dbs haven't been created yet, we want to destroy the whole db
13+
# cluster and recreate it with proper utf8 support. From http://ph.ly/pg
14+
sudo: yes
15+
sudo_user: postgres
16+
shell: pg_createcluster --start -e {{ pg_encoding }} {{ pg_version }} {{ pg_cluster }}
17+
tags:
18+
- postgres

‎provision/roles/postgresql/templates/master.conf.j2

Lines changed: 582 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# PostgreSQL Client Authentication Configuration File
2+
#
3+
# Generated by Ansible
4+
# All handmade changes can be rewited!
5+
#
6+
# Allow any user on the local system to connect to any database with
7+
# any database user name using Unix-domain sockets (the default for local
8+
# connections).
9+
#
10+
# TYPE DATABASE USER ADDRESS METHOD
11+
12+
# Default:
13+
{% for connection in pg_cfg_pg_hba_default %}
14+
# {{ connection.comment }}
15+
{{ connection.type }} {{ connection.database }} {{ connection.user }} {{ connection.address }} {{ connection.method }}
16+
{% endfor %}
17+
18+
# Passwored hosts
19+
{% for host in pg_cfg_pg_hba_passwd_hosts %}
20+
# {{ connection.comment }}
21+
host all all {{ host }} password
22+
{% endfor %}
23+
24+
# Trusted hosts
25+
{% for host in pg_cfg_pg_hba_trust_hosts %}
26+
# {{ connection.comment }}
27+
host all all {{ host }} trust
28+
{% endfor %}
29+
30+
# User custom
31+
{% for connection in pg_cfg_pg_hba_custom %}
32+
# {{ connection.comment }}
33+
{{ connection.type }} {{ connection.database }} {{ connection.user }} {{ connection.address }} {{ connection.method }}
34+
{% endfor %}

‎provision/roles/postgresql/vars/main.yml

Lines changed: 533 additions & 0 deletions
Large diffs are not rendered by default.

‎provision/roles/redis/tasks/main.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
- name: update apt
3+
apt: update_cache=yes
4+
sudo: yes
5+
6+
- name: ensure packages installed
7+
apt: pkg={{ item }} state=latest
8+
sudo: yes
9+
with_items:
10+
- make
11+
- build-essential
12+
- tcl8.5
13+
14+
- name: download latest stable redis
15+
get_url: url=http://download.redis.io/redis-stable.tar.gz dest=/tmp/redis-stable.tar.gz
16+
17+
- name: untar redis
18+
command: tar zxf /tmp/redis-stable.tar.gz -C /tmp
19+
20+
- name: build redis
21+
command: make -C /tmp/redis-stable
22+
23+
- name: create redis group
24+
group: name=redis state=present system=yes
25+
sudo: yes
26+
27+
- name: create redis user
28+
user: name=redis group=redis createhome=no shell=/bin/false system=yes state=present
29+
sudo: yes
30+
31+
- name: make sure that /etc/redis exists
32+
file: path=/etc/redis state=directory mode=0755
33+
sudo: yes
34+
35+
- name: make sure that /var/db/redis exists
36+
file: path=/var/db/redis state=directory mode=0755 group=redis owner=redis
37+
sudo: yes
38+
39+
- name: make sure redis.log file exists
40+
copy: src=roles/redis/templates/redis.log dest=/var/log/redis.log owner=redis group=redis mode=0644
41+
sudo: yes
42+
43+
- name: copy upstart file
44+
copy: src=roles/redis/templates/upstart.conf dest=/etc/init/redis.conf
45+
sudo: yes
46+
47+
- name: copy redis.conf file
48+
copy: src=roles/redis/templates/redis.conf dest=/etc/redis/redis.conf group=redis owner=redis
49+
sudo: yes
50+
51+
- name: copy custom template
52+
template: src=roles/redis/templates/redis.local.conf.j2 dest=/etc/redis/redis.local.conf group=redis owner=redis
53+
sudo: yes
54+
55+
- name: copy redis-local script
56+
template: src=roles/redis/templates/redis-local.j2 dest=/usr/local/bin/redis-local mode=0755
57+
sudo: yes
58+
59+
- name: installing redis binaries
60+
command: cp /tmp/redis-stable/src/{{ item }} /usr/local/bin
61+
with_items:
62+
- redis-server
63+
- redis-cli
64+
- redis-check-aof
65+
- redis-check-dump
66+
sudo: yes
67+
68+
- name: cleaning up build files
69+
command: rm -rf /tmp/{{ item }}
70+
with_items:
71+
- redis-stable
72+
- redis-stable.tar.gz
73+
sudo: yes
74+
75+
- name: ensure redis service is restarted
76+
service: name=redis state=restarted
77+
sudo: yes
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
### auth with local redis install
4+
5+
{% if redis_password %}
6+
7+
redis-cli -p {{ redis_port }} -a {{ redis_password }}
8+
9+
{% else %}
10+
11+
redis-cli -p {{ redis_port }}
12+
13+
{% endif %}

‎provision/roles/redis/templates/redis.conf

Lines changed: 560 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#upstart takes care of this
2+
daemonize no
3+
4+
pidfile /var/run/redis.pid
5+
port {{ redis_port }}
6+
timeout 0
7+
loglevel notice
8+
logfile /var/log/redis.log
9+
dir /var/db/redis/
10+
11+
{% if redis_password %}
12+
13+
requirepass {{ redis_password }}
14+
15+
{% endif %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
redis.log
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
description "redis server"
2+
3+
start on runlevel [23]
4+
stop on shutdown
5+
6+
exec sudo -u redis /usr/local/bin/redis-server /etc/redis/redis.conf
7+
8+
respawn
9+
respawn limit 5 60

‎provision/roles/redis/vars/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
redis_port: 6379
2+
redis_password: "Brainattica"

‎provision/vars.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
project_name: jwt_authentication
2+
application_name: jwt_authentication
3+
4+
db_user: brainattica
5+
db_name: project_db
6+
db_password: BrainAttic
7+
db_name_tests: db_tests
8+
9+
10+
application_dir: /home/vagrant/go/src/api.jwt.authentication/
11+
go_path: /home/vagrant/go

‎routers/authentication.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package routers
2+
3+
import (
4+
"api.jwt.auth/controllers"
5+
"api.jwt.auth/core/authentication"
6+
"github.com/codegangsta/negroni"
7+
"github.com/gorilla/mux"
8+
)
9+
10+
func SetAuthenticationRoutes(router *mux.Router) *mux.Router {
11+
router.HandleFunc("/token-auth", controllers.Login).Methods("POST")
12+
router.Handle("/refresh-token-auth", negroni.New(negroni.HandlerFunc(authentication.RequireTokenAuthentication), negroni.HandlerFunc(controllers.RefresfhToken))).Methods("GET")
13+
return router
14+
}

‎routers/hello.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package routers
2+
3+
import (
4+
"api.jwt.auth/controllers"
5+
"api.jwt.auth/core/authentication"
6+
"github.com/codegangsta/negroni"
7+
"github.com/gorilla/mux"
8+
)
9+
10+
func SetHelloRoutes(router *mux.Router) *mux.Router {
11+
router.Handle("/test/hello", negroni.New(negroni.HandlerFunc(authentication.RequireTokenAuthentication), negroni.HandlerFunc(controllers.HelloController))).Methods("GET")
12+
return router
13+
}

‎routers/router.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package routers
2+
3+
import (
4+
"github.com/gorilla/mux"
5+
)
6+
7+
func InitRoutes() *mux.Router {
8+
router := mux.NewRouter()
9+
router = SetHelloRoutes(router)
10+
router = SetAuthenticationRoutes(router)
11+
return router
12+
}

‎server.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"api.jwt.auth/routers"
5+
"github.com/codegangsta/negroni"
6+
"net/http"
7+
)
8+
9+
func main() {
10+
router := routers.InitRoutes()
11+
n := negroni.Classic()
12+
n.UseHandler(router)
13+
http.ListenAndServe(":5000", n)
14+
}

‎services/models/users.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models
2+
3+
type User struct {
4+
Username string `json:"username" form:"username""`
5+
Password string `json:"password" form:"password"`
6+
}

0 commit comments

Comments
 (0)
Please sign in to comment.