App.vue 5.58 KB
Newer Older
1
2
<template>
  <div id="app">
3
4
5
6
7
8
    <app-header
      :modules="modules"
      :user="user"
    />
    <router-view />
    <app-footer />
9
10
11
12
  </div>
</template>

<script>
13
14
import 'bootstrap/dist/css/bootstrap.css'
import 'bootstrap-vue/dist/bootstrap-vue.css'
15
import oidc from 'oidc-client'
16
17
import header from './components/Header.vue'
import footer from './components/Footer.vue'
18
import axios from 'axios'
19
20

export default {
21
  name: 'App',
22
23
24
25
26
27
  components: {
    'app-header': header,
    'app-footer': footer
  },
  data () {
    return {
28
29
30
31
32
33
      modules_logged_out: [
        { slug: 'home', title: 'Home' },
        { slug: 'credits', title: 'Credits' },
        { slug: 'debug', title: 'Debug' }
      ],
      modules_logged_in: [
34
35
36
37
38
39
40
        { slug: 'home', title: 'Home', superusersOnly: false },
        { slug: 'shows', title: 'Shows', superusersOnly: false },
        { slug: 'files', title: 'Files & Playlists', superusersOnly: false },
        { slug: 'emissions', title: 'Emissions', superusersOnly: true },
        { slug: 'settings', title: 'Settings', superusersOnly: false },
        { slug: 'credits', title: 'Credits', superusersOnly: false },
        { slug: 'debug', title: 'Debug', superusersOnly: true }
41
      ],
42
43
44
45
      user: {
        name: '',
        email: '',
        access_token: '',
46
        expires_at: 0,
47
48
        logged_in: false,
        steeringUser: null
49
50
      },
      userOIDC: null,
51
      oidcmgr: new oidc.UserManager({
52
        userStore: new oidc.WebStorageStateStore(),
53
        authority: process.env.VUE_APP_API_STEERING_OIDC_URI,
54
        // the client id has to be a string, therefore we add the + ''
55
56
        client_id: process.env.VUE_APP_OIDC_CLIENT_ID,
        redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI,
57
58
        silent_redirect_uri: 'http://localhost:8080/oidc_callback_silentRenew.html',
        popup_redirect_uri: 'http://localhost:8080/oidc_callback_popupRenew.html',
59
        accessTokenExpiringNotificationTime: process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION,
60
61
        response_type: 'id_token token',
        scope: 'openid profile email',
62
        post_logout_redirect_uri: process.env.VUE_APP_API_STEERING_OIDC_REDIRECT_URI_POSTLOGOUT,
63
        loadUserInfo: true
64
      })
65
    }
66
67
68
  },
  computed: {
    modules: function () {
69
70
      if (this.user.logged_in === true) { return this.modules_logged_in }
      else { return this.modules_logged_out }
71
72
    }
  },
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  mounted () {
    // TODO: remove oidc logging after thorough testing
    oidc.Log.logger = console
    let self = this
    this.oidcmgr.events.addAccessTokenExpiring(function () {
      console.log('starting silent access_token renewal')
      self.oidcmgr.signinSilent().then(function (user) {
        self.user.access_token = user.access_token
        console.log(self.user.access_token)
      }).catch(function (err) {
        console.log(err)
        alert('Your OpenID access token could not be renewed automatically.\n' +
          'You will be logged out in ' + process.env.VUE_APP_API_STEERING_OIDC_EXPIRE_NOTIFICATION + ' seconds.')
      })
    })
    this.oidcmgr.events.addAccessTokenExpired(function () {
      console.log('expired!')
      self.signOut()
    })
    this.getOIDCUser()
  },
94
95
  methods: {
    signIn () {
96
      this.oidcmgr.signinRedirect().catch(function (err) {
97
98
99
100
101
        console.log(err)
      })
    },
    signOut () {
      let self = this
102
      this.oidcmgr.signoutRedirect().then(function (resp) {
103
104
105
106
107
108
        self.user.logged_in = false
        console.log('signed out', resp)
      }).catch(function (err) {
        console.log(err)
      })
    },
109
    getSteeringUser () {
110
      axios.get(process.env.VUE_APP_API_STEERING + 'users/', {
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.user.access_token }
      }).then(response => {
        if (response.data.length === 0) {
          alert('No user profile data provided by steering backend!')
        } else if (response.data.length === 1) {
          this.user.steeringUser = response.data[0]
        } else {
          // in case we are a superuser, we get all users returned
          // so we have to iterate through the user list to find out own profile
          for (var u in response.data) {
            if (response.data[u].username === this.user.name) {
              this.user.steeringUser = response.data[u]
              break
            }
          }
        }
      }).catch(error => {
        alert('There was an error fetching user data from the steering backend: ' + error)
      })
    },
132
    getOIDCUser () {
133
      let self = this
134
135
      this.oidcmgr.getUser().then(function (user) {
        if (user == null) {
136
137
138
139
140
          self.user.logged_in = false
          self.user.name = ''
          self.user.email = ''
          self.user.access_token = ''
        } else {
141
142
          // TODO: check user.expires_at
          // if token already expired try to get a new one or mark the user as logged out
143
          self.setUserProperties(user)
144
          self.getSteeringUser()
145
146
147
148
        }
      }).catch(function (err) {
        console.log(err)
      })
149
150
151
152
153
154
155
    },
    setUserProperties (user) {
      this.userOIDC = user
      this.user.logged_in = true
      this.user.name = user.profile.nickname
      this.user.email = user.profile.email
      this.user.access_token = user.access_token
156
      // TODO: remove debug info after thorough testing
157
158
159
      console.log(new Date(user.expires_at * 1000).toString())
      console.log(new Date(user.expires_at * 1000).toUTCString())
      console.log(user.access_token)
160
    }
161
162
163
164
165
166
  }
}
</script>

<style>
#app {
167
  color: #2e3436;
168
}
169
/*
170
171
172
173
174
#app .content-width {
  margin: auto;
  width: 920px;
  max-width: 920px;
}
175
*/
176
</style>