npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng2-oauth2

v0.0.5

Published

Provide OAuth2 and OpenId Connect support for Angular2

Downloads

4

Readme

ng2-oauth2

This package provides a Oauth2 and OpenId Connect implicit flow module for Angular2.

This is a work in progress !!

It has been largely ispired from :

Usage example

Into main.ts:

import { OAUTH2_PROVIDERS } from 'ng2-oauth2/ng2-oauth2';
...
bootstrap(
	...
	OAUTH2_PROVIDERS
);

Into your main application component:

import { Oauth2Service } from 'ng2-oauth2/ng2-oauth2';

export class AppComponent {
	constructor(private oauth2service: Oauth2Service) {
		this.oauth2service.init({
            site: 'https://my.oauth.server.site.com',
            redirectUri: 'http://localhost:3000',
            clientId: 'xxxxxxxxxxxxxxxxxxxxxx',
            wellKnown: true,
            issuer: 'https://my.oauth.server.site.com',
            scope: 'openid profile',
            responseType: 'token id_token',
            useNonce: true,
            profileUri: 'https://my.oauth.server.site.com/oauth/me/',
            destroyTokenPath: '/oauth/destroy/'
        });
	}
}

Into your authentication component (I use it in my navbar):

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Oauth2Service } from 'ng2-oauth2/ng2-oauth2';

...

@Component({
    selector: 'my-navbar',
    templateUrl: 'app/shared/navbar/navbar.html',
    directives: [ROUTER_DIRECTIVES, CORE_DIRECTIVES]
})
export class NavbarComponent implements OnInit, OnDestroy {
	public profile = {};
    public loggedin = false;

    // Injects the service into the component
    constructor(private oAuthService: Oauth2Service, private location: Location) {}

	ngOnInit() {
		// Suscribe to interesting events
		Oauth2Service.Authorized.subscribe(item => {
            console.log('Authorized event captured ', item.token);
            this.loggedin = true;
        });
        Oauth2Service.LoggedOut.subscribe(item => {
            console.log('Logged out event captured');
            this.loggedin = false;
            this.profile = {};
        });
        Oauth2Service.Profile.subscribe(item => {
            console.log('Profile event captured');
            this.profile = item.profile;
        });

        // At load, tries to login (If contains fragments with "access_token")
        this.oAuthService.tryLogin();
    }

    ngOnDestroy() {
        Oauth2Service.LoggedIn.unsubscribe();
        Oauth2Service.Profile.unsubscribe();
        Oauth2Service.Authorized.unsubscribe();
    }

    login() {
        this.oAuthService.login(this.location.href);
    }

    logout() {
        this.oAuthService.logout();
    }
}

Initialization parameters

| Param | type | default | Usage | |-------|------|---------|-------| | site | string | undefined | Oauth / OpenId Connect site | | redirectUri | string | undefined | Uri for redirection after login | | authorizePath | string | '/oauth/authorize' | authorize endpoint | | tokenPath | string | '/oauth/token' | token endpoint | | destroyTokenPath | string | undefined | token destruction endpoint (called with header 'Authorization: Bearer %access_token%') | | responseType | string | 'token' | oauth response type ('token id_token' for OpenId Connect) | | storage | Storage | sessionStorage | Storage for token, OIDC configuration. Can be sessionStorage or localStorage | | profileUri | string | undefined | endpoint to get user Profile in JSON format | | generateState | boolean | false | generate or not a state param for request | | useNonce | boolean | false | generate or not a nonce param for request (mandatory for OpenId Connect) | | issuer | string | undefined | Server where to get OpenIdConnect configuration | | wellKnown | bool | false | use OpenId Connect .well-known/configuration endpoint to get informations about the server | | logOutPath | string | undefined | If set, user will be redirected to this url to logout the Oauth server | | state | string | undefined | set the state to this value in the oauth request | | nonce | string | undefined | set the nonce to this value in the oauth request |

Observable events

Events are triggered through static objects of service.

For instance:

// Emit event
Oauth2Service.Profile.emit({profile: profileObject});

// Subscribe to event
Oauth2Service.Profile.subscribe(
	(item) => console.log('Profile is: ', item.profile);

// Unsubscribe to event
Oauth2Service.Profile.unsubscribe();

| Event | Observed object | Description | |-------|-----------------|-------------| | Login | {} | Triggered when user calls the "login()" function | | Logout | {} | Triggered when user calls the "logout()" function | | LoggedIn | {token: tokenObject} | Triggered when user successfully logs in | | Authorized | {token: tokenObject} | Triggered when user is authenticated (from session or hash parameters) | | LoginError | {} | Triggered when user login fails | | LoggedOut | {} | Triggered when user is not authenticated (no hash parameters, no session data) | | TokenExpired | {} | Triggered when the user token is expired | | TokenDestroyed | {} | Triggered when the user token is destroyed | | TokenDestroyError | {error: errorName, error_description: errorDescriptionString } | Triggered when the user token destruction fails | | Profile | {profile: profileObject} | Triggered when the user profile is set from the profileUri | | ProfileError | {error: errorName, error_description: errorDescriptionString } | Triggered when the user profile fails | | OIDCConfig | {config: configObject} | Triggered when the OIDC config is set from the server | | OIDCKeys | {config: configObject} | Triggered when the OIDC keys are set from the server | | OIDCConfigError | {error: errorName, error_description: errorDescriptionString } | Triggered when the OIDC config fails | | OIDCKeysError | {error: errorName, error_description: errorDescriptionString } | Triggered when the OIDC keys fails |