forked from alexhermida/taiga-contrib-oidc-auth
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathoidc_auth.coffee
83 lines (64 loc) · 2.7 KB
/
oidc_auth.coffee
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
module = angular.module('taigaContrib.oidcAuth', [])
OIDCLoginButtonDirective = ($window, $params, $location, $config, $events, $confirm, $auth, $navUrls, $rootScope) ->
# Login or register a user with their OIDC account.
link = ($scope, $el, $attrs) ->
loginSuccess = ->
if $params.next and $params.next != $navUrls.resolve("login")
nextUrl = $params.next
else
nextUrl = $navUrls.resolve("home")
$events.setupConnection()
$auth.removeToken();
data = JSON.parse($params.data);
user = $auth.model.make_model("users", data);
$auth.setToken(user.auth_token);
$auth.setRefreshToken(user.refresh)
$auth.setUser(user);
$rootScope.$broadcast("auth:login", user)
$window.location.href = nextUrl
loginError = ->
error_description = $params.error_description
$location.search("type", null)
$location.search("error", null)
$location.search("error_description", null)
if error_description
$confirm.notify("light-error", error_description)
else
$confirm.notify("light-error", "Our Oompa Loompas have not been able to get you
credentials from GitHub.") #TODO: i18n
loginWithOIDCAccount = ->
type = $params.type
auth_token = $params.auth_token
return if not (type == "oidc")
if $params.error
loginError()
else
loginSuccess()
loginWithOIDCAccount()
$el.on "click", ".button-auth", (event) ->
if $params.next and $params.next != $navUrls.resolve("login")
nextUrl = $params.next
else
nextUrl = $navUrls.resolve("home")
base_url = $config.get("api", "/api/v1/").split('/').slice(0, -3).join("/")
url = urljoin(
base_url,
$config.get("oidcMountPoint", "/oidc"),
"authenticate/"
)
url += "?next=" + nextUrl
$window.location.href = url
$scope.$on "$destroy", ->
$el.off()
# Template context
$scope.buttonText = $config.get("oidcButtonText", "OpenID Connect")
$scope.buttonImage = $config.get("oidcButtonImage", "logo.gif")
return {
link: link
restrict: "EA"
template: ""
}
module.directive("tgOidcLoginButton", [
"$window", '$routeParams', "$tgLocation", "$tgConfig", "$tgEvents",
"$tgConfirm", "$tgAuth", "$tgNavUrls", "$rootScope",
OIDCLoginButtonDirective])