forked from vlang/gitly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.v
110 lines (104 loc) · 3.2 KB
/
oauth.v
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
106
107
108
109
110
// Copyright (c) 2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license that can be found in the LICENSE file.
module main
import vweb
import json
import net.http
struct OAuthRequest {
client_id string
client_secret string
code string
state string
}
struct GitHubUser {
username string [json: 'login']
name string
email string
avatar string [json: 'avatar_url']
}
pub fn (mut app App) oauth() vweb.Result {
code := app.query['code']
state := app.query['state']
if code == '' {
app.security_log(user_id: app.user.id, kind: .empty_oauth_code)
app.info('Code is empty')
return app.r_home()
}
csrf := app.get_cookie('csrf') or { return app.r_home() }
if csrf != state || csrf == '' {
app.security_log(
user_id: app.user.id
kind: .wrong_oauth_state
arg1: 'csrf=$csrf'
arg2: 'state=$state'
)
return app.r_home()
}
req := OAuthRequest{
client_id: app.settings.oauth_client_id
client_secret: app.settings.oauth_client_secret
code: code
state: csrf
}
d := json.encode(req)
resp := http.post_json('https://github.com/login/oauth/access_token', d) or {
app.info(err)
return app.r_home()
}
mut token := resp.text.find_between('access_token=', '&')
mut request := http.new_request(.get, 'https://api.github.com/user', '') or {
app.info(err)
return app.r_home()
}
request.add_header('Authorization', 'token $token')
user_js := request.do() or {
app.info(err)
return app.r_home()
}
if user_js.status_code != 200 {
app.info(user_js.status_code.str())
app.info(user_js.text)
return app.text('Received $user_js.status_code error while attempting to contact GitHub')
}
gh_user := json.decode(GitHubUser, user_js.text) or { return app.r_home() }
println('gh user:')
println(user_js.text)
println(gh_user)
if gh_user.email.trim_space().len == 0 {
app.security_log(user_id: app.user.id, kind: .empty_oauth_email, arg1: user_js.text)
app.info('Email is empty')
// return app.r_home()
}
mut user := app.find_user_by_github_username(gh_user.username) or { User{} }
if !user.is_github {
// Register a new user via github
app.security_log(user_id: user.id, kind: .registered_via_github, arg1: user_js.text)
app.add_user(gh_user.username, '', [gh_user.email], true)
user = app.find_user_by_github_username(gh_user.username) or { return app.r_home() }
app.update_user_avatar(gh_user.avatar, user.id)
}
ip := app.client_ip(user.id.str()) or {
println('Can not fetch ip')
return app.r_home()
}
app.auth_user(user, ip)
app.security_log(user_id: user.id, kind: .logged_in_via_github, arg1: user_js.text)
return app.r_home()
}
fn (mut app App) load_settings() {
data := sql app.db {
select from GitlySettings limit 1
}
app.settings = data
}
fn (mut app App) update_settings() {
id := app.settings.id
oauth_client_id := app.settings.oauth_client_id
oauth_client_secret := app.settings.oauth_client_secret
only_gh_login := if app.settings.only_gh_login { 1 } else { 0 }
repo_storage_path := app.settings.repo_storage_path
sql app.db {
update GitlySettings set oauth_client_id = oauth_client_id, oauth_client_secret = oauth_client_secret,
repo_storage_path = repo_storage_path where id == id
}
}