Skip to content

Commit 586ac43

Browse files
committed
initial
0 parents  commit 586ac43

51 files changed

Lines changed: 1364 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.pyc
2+
*.pyo
3+
*.egg-info
4+
/dist
5+
/build

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.6.2

.travis.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: python
2+
3+
python:
4+
- "2.6"
5+
- "2.7"
6+
- "3.3"
7+
- "3.4"
8+
- "3.5"
9+
- "3.6"
10+
11+
env:
12+
- REQUESTS="requests" # latest
13+
- REQUESTS="requests==2.5" # min version of requests library
14+
15+
install: pip install $REQUESTS
16+
17+
script: python setup.py test

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2017 Castle
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Python SDK for Castle
2+
3+
[![Build Status](https://travis-ci.org/castle/castle-python.png)](https://travis-ci.org/castle/castle-python)
4+
5+
**[Castle](https://castle.io) adds real-time monitoring of your authentication stack, instantly notifying you and your users on potential account hijacks.**
6+
7+
This module has been tested with Python 2.6, 2.7, 3.3, 3.4, 3.5 and 3.6
8+
9+
## Installation
10+
11+
```bash
12+
pip install castle-python
13+
```

RELEASING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Releasing
2+
=========
3+
4+
1. Update `VERSION` in `castle/version.py` to the new version.
5+
2. Update the `HISTORY.md` for the impending release.
6+
3. `git commit -am "Prepare for release X.Y.Z."` (where X.Y.Z is the new version)
7+
4. `git tag -a X.Y.Z -m "Version X.Y.Z"` (where X.Y.Z is the new version).
8+
5. `make dist`.

castle/__init__.py

Whitespace-only changes.

castle/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from castle.request import Request
2+
from castle.response import Response
3+
4+
5+
class Api(object):
6+
def __init__(self):
7+
self.req = Request({'Content-Type': 'application/json'})
8+
9+
def request(self, command):
10+
return self.req.build_query(command.method, command.endpoint, command.data)
11+
12+
def response(self, req):
13+
return Response(req).call()
14+
15+
def call(self, command):
16+
return self.response(self.request(command))

castle/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from castle.configuration import configuration
2+
from castle.api import Api
3+
from castle.context.default import ContextDefault
4+
from castle.context.merger import ContextMerger
5+
from castle.commands.authenticate import CommandsAuthenticate
6+
from castle.commands.identify import CommandsIdentify
7+
from castle.commands.track import CommandsTrack
8+
from castle.exceptions import InternalServerError
9+
from castle.failover_response import FailoverResponse
10+
11+
12+
class Client(object):
13+
def __init__(self, request, options):
14+
self.options = options or dict()
15+
self.do_not_track = self.default_tracking()
16+
self.context = self.setup_context(request)
17+
self.api = Api()
18+
19+
def identify(self, options):
20+
if not self.tracked():
21+
return
22+
return self.api.call(CommandsIdentify(self.context).build(options))
23+
24+
def authenticate(self, options):
25+
if self.tracked():
26+
try:
27+
response = self.api.call(CommandsAuthenticate(self.context).build(options))
28+
response.update(failover=False, failover_reason=None)
29+
return response
30+
except InternalServerError as exception:
31+
return self.failover(options, exception)
32+
else:
33+
return FailoverResponse(options['user_id'], 'allow', 'Castle set to do not track.').call()
34+
35+
def track(self, options):
36+
if not self.tracked():
37+
return
38+
return self.api.call(CommandsTrack(self.context).build(options))
39+
40+
def disable_tracking(self):
41+
self.do_not_track = True
42+
43+
def enable_tracking(self):
44+
self.do_not_track = False
45+
46+
def tracked(self):
47+
return not self.do_not_track
48+
49+
def default_tracking(self):
50+
return self.options['do_not_track'] if 'do_not_track' in self.options else False
51+
52+
def setup_context(self, request):
53+
default_context = ContextDefault(request, self.options.get('cookies', dict())).call()
54+
return ContextMerger(default_context).call(self.options.get('context', dict()))
55+
56+
def failover(self, options, exception):
57+
if configuration.failover_strategy != 'throw':
58+
return FailoverResponse(options['user_id'], None, exception.__class__.__name__).call()
59+
raise exception

castle/command.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from collections import namedtuple
2+
3+
Command = namedtuple('Command', ['method', 'endpoint', 'data'])

0 commit comments

Comments
 (0)