2.1.2 • Published 5 years ago

azuread-token-auth v2.1.2

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

Azure Token Auth

Azure Token Auth is a Javascript integration with AzureAD authenticating a user and setting recieved tokens to local storage.

  • Authenticates you with Azure AD using oauth2
  • Redirect login is used if the user has never logged in
  • Background login requesting refresh tokens, which replaces current token in local storage
  • Logout of AzureAD

Required Environment Variables

VARDiscription
VUE_APP_TENANTTennant found in Azure configuration
VUE_APP_CALLBACKCallback URL for post login
VUE_APP_CLIENTIDClient ID found in Azure configuration

Functions

Function
login()User will be logged in with either redirect or background styles. The strings "redirect", and "background" will be returned depending on what style is used
logout()User will be ridireced to Azure AD logout

Usage

Import

import {login} from 'azuread-token-auth'

####Sample implementation in Vue.js

In .env.local:

NODE_ENV=<development, production, etc.>
VUE_APP_PROTOCOL=<http or https>
VUE_APP_API=<URL to API>
VUE_APP_SECRET=<Client Secret>
VUE_APP_CLIENTID=<Client ID>
VUE_APP_TENANT=<Tenant ID>
VUE_APP_CALLBACK=<Callback URL>

In App.vue:

export default {
  name: 'app',
  components: {...},
  created(){
    this.$store.dispatch('appSetup')
  },
}

In store.js:

import Vue from 'vue'
import Vuex from 'vuex'

import {login} from 'azuread-token-auth'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        apiToken: '',
    },
    mutations: {
        setApiToken(state, value){
            state.apiToken = value
          },
    },
    actions: {
        // eslint-disable-next-line no-unused-vars
        appSetup({ commit, getters, dispatch }) {
            dispatch('getToken').then(() => {
                // eslint-disable-next-line no-unused-vars
                const token = getters.getTokenFromLocalStorage
                // TODO: Interact with token here ...
            })
        },
        // eslint-disable-next-line no-unused-vars
        async tokenRefresh({ commit, dispatch }) {
            dispatch('getToken').then(() => { 
				//TODO: Do any post-refresh actions here ...
            })
        },
        async getToken({ commit, getters }) {
            return new Promise((resolve, reject) => {
                const url = window.location.href
                const substring = 'id_token'
                let localToken = getters.getTokenFromLocalStorage

                // First, try to get the token from the URL ...
                if(url.toString().indexOf(substring) !== -1) {
                    // eslint-disable-next-line no-useless-escape
                    const access_token = url.match(/\#(?:id_token)\=([\S\s]*?)\&/)[1];
                    localStorage.setItem("Auth.idToken", access_token)
                    if(access_token) {
                        localToken = access_token
                        commit('setApiToken', localToken)
                        resolve(localToken)
                    }
                // If we couldn't get the token from the URL, trying getting it from local storage ...
                } else if(localToken) {
                    commit('setApiToken', localToken)
                    resolve(localToken)
                // If we couldn't get the token from the URL or local storage, get a new token ...
                } else {
                    login().then(() => {
                        localToken = getters.getTokenFromLocalStorage
                        if(localToken){
                            commit('setApiToken', localToken)
                            resolve(localToken)
                        }else{
                            reject("error getting localStorage") 
                        }
                    })
                }
            })
        },
    },
    },
    getters: {
        getTokenFromLocalStorage: () => {
            return localStorage.getItem('Auth.idToken')
        },
    }
})
2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.7

5 years ago

2.0.6

5 years ago

2.0.5

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.9.9

5 years ago

1.9.8

5 years ago

1.9.7

5 years ago

1.9.6

5 years ago

1.9.5

5 years ago

1.9.4

5 years ago

1.9.3

5 years ago

1.9.2

5 years ago

1.9.1

5 years ago

1.9.0

5 years ago

1.8.9

5 years ago

1.8.8

5 years ago

1.8.7

5 years ago

1.8.6

5 years ago

1.8.5

5 years ago

1.8.4

5 years ago

1.8.3

5 years ago

1.8.2

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.9

5 years ago

1.7.8

5 years ago

1.7.7

5 years ago

1.7.6

5 years ago

1.7.5

5 years ago

1.7.4

5 years ago

1.7.3

5 years ago

1.7.2

5 years ago

1.7.1

5 years ago

1.7.0

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.9

5 years ago

1.4.8

5 years ago

1.4.7

5 years ago

1.4.6

5 years ago

1.4.5

5 years ago

1.4.4

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.9

5 years ago

1.2.8

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago