-
Notifications
You must be signed in to change notification settings - Fork 2
/
04_authentication_example.py
105 lines (62 loc) · 2.94 KB
/
04_authentication_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""04 Authentication example.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1e-8bCEDVlQjOCWH0w4iEuRBS-o6c9QEN
# 4 Authentication and describing access Policies
So far all API access have been http GETs with annonymous (not logged in) access.
We will now look at authenticating, to access both hidden items and create things.
"""
import requests
import json
import string
import getpass
"""The base_url now points to a special SEEK instance that we can do what we like with."""
base_url = 'https://sandbox3.fairdomhub.org'
"""the SEEK currently uses HTTP Authentication for authorization. This can be handled in Python using a requests Session object, through which all requests are made. This session also holds reusable details like the headers.
We have future plans to also handle a token based authentication system.
"""
headers = {"Accept": "application/vnd.api+json",
"Accept-Charset": "ISO-8859-1"}
session = requests.Session()
session.headers.update(headers)
session.auth = (input('Username:'), getpass.getpass('Password'))
def json_for_resource(type, id):
r = session.get(base_url + "/" + type + "/" + str(id), headers=headers)
if (r.status_code != 200):
print(r.json())
r.raise_for_status()
return r.json()
"""Without authenticating an error will occur:
- 403 Client Error. 403 is the standard HTTP CODE for unauthorized access.
The JSON response will also provide error details.
Once authenticated you should see the title of the file.
"""
data_file_id = 1
result = json_for_resource('data_files',data_file_id)
title = result['data']['attributes']['title']
title
"""The attributes of the data file.
**Note** that there is a new 'policy' block.
"""
result['data']['attributes']
"""The policy block is only visible if you have 'manage' rights to a resource. It contains information about the sharing permissions.
* Possible access rights are:
* no_access
* view
* download
* edit
* manage
* The top level 'access' attribute indicates access for unregistered anonymous users (Public)
* The permissions lists special access to specific resources - Programme, Project, Instituion or Person. Programme, Project, Institution corresponds to whether yu are a member of that group.
In this case:
* There is no access to all anonymous users
* Members of Project 1 https://sandbox3.fairdomhub.org/projects/1 can download the item
* Person 1 https://sandbox3.fairdomhub.org/people/1 has been granted special manage rights
The submitter of the item isn't listed, but always has manage rights.
"""
result['data']['attributes']['policy']
"""# Exercise 4
* Register an account with https://sandbox3.fairdomhub.org - don't re-use an existing username or password or worry about anything too secure. Let Stuart or Jacky know when you've done so they can add you to the project
* Run the example using your login and password
"""