Skip to content

Auth API #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
2,990 changes: 2,990 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,20 @@
},
"homepage": "https://github.com/shprink/wp-api-angular",
"peerDependencies": {
"@angular/core": "^4.0.0",
"@angular/http": "^4.0.0"
"@angular/core": "^4.4.6",
"@angular/http": "^4.4.6"
},
"dependencies": {
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/common": "^4.4.6",
"@angular/compiler": "^4.4.6",
"@angular/core": "^4.4.6",
"@angular/http": "^4.4.6",
"@angular/platform-browser": "^4.4.6",
"@angular/platform-browser-dynamic": "^4.4.6",
"rxjs": "^5.0.0"
},
"devDependencies": {
"@angular/compiler-cli": "^4.0.0",
"@angular/compiler-cli": "^4.4.6",
"babel-core": "^6.10.4",
"babel-loader": "^6.2.4",
"core-js": "^2.4.0",
Expand Down
59 changes: 59 additions & 0 deletions src/Auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Injectable, Inject } from '@angular/core';
import { Headers, Http } from '@angular/http';

// Need to import interfaces dependencies
// Bug TypeScript https://github.com/Microsoft/TypeScript/issues/5938
import { Observable } from 'rxjs/Observable';
import { RequestOptionsArgs } from '@angular/http/src/interfaces';
import { Response } from '@angular/http/src/static_response';

import { WpApiParent } from './Parent';
import { AuthSession } from './AuthSession';

import { WpApiLoader } from './Loaders';

import { IWpApiAuth, ICredentials, IAuthCredentials } from './interfaces';

/**
* Authentication implementation of WordPress authentication.
* Uses AuthSession for storing authentication credentials.
* @see AuthSession
* @author karrirasinmaki
*/
@Injectable()
export class WpApiAuth extends WpApiParent implements IWpApiAuth {

constructor(
public wpApiLoader: WpApiLoader,
public http: Http,
) {
super(wpApiLoader, http);
}

protected getWebServiceUrl(postfix: string): string {
return super.getWebServiceUrl(postfix).replace(this.wpApiLoader.namespace, '/jwt-auth/v1');
}

saveSession(credentials: ICredentials) {
AuthSession.saveSession(credentials);
}
getSession(): ICredentials {
return AuthSession.getSession();
}
removeSession() {
AuthSession.removeSession();
}

auth(authCredentials: IAuthCredentials, options = { headers: new Headers() }): Observable<Response> {
if (!options.headers) {
options.headers = new Headers();
}
options.headers.append('Authorization', '');
return this.httpPost(`/token`, authCredentials, options);
}
validate(options = {}): Observable<Response> {
return this.httpPost(`/token/validate`, {}, options);
}

}

17 changes: 17 additions & 0 deletions src/AuthSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ICredentials } from './interfaces';

/**
* Stores auth session.
* @author karrirasinmaki
*/
export class AuthSession {
static saveSession(credentials: ICredentials) {
localStorage.setItem('credentials', JSON.stringify(credentials));
}
static getSession(): ICredentials {
return JSON.parse(localStorage.getItem('credentials'));
}
static removeSession() {
localStorage.removeItem('credentials');
}
}
8 changes: 1 addition & 7 deletions src/Comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiComments {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(commentId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(commentId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(commentId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiComments } from './interfaces';

@Injectable()
export class WpApiComments extends WpApiParent implements IWpApiComments {
Expand Down
8 changes: 1 addition & 7 deletions src/Custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiCustom {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(customId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(customId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(customId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiCustom } from './interfaces';

export class Custom extends WpApiParent implements IWpApiCustom {
constructor(
Expand Down
12 changes: 10 additions & 2 deletions src/Loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ import { stripTrailingSlash } from './utils';

export abstract class WpApiLoader {
abstract getWebServiceUrl(postfix: string): string;
abstract baseUrl: string;
abstract namespace: string;
}

export class WpApiStaticLoader implements WpApiLoader {
completeUrl: string;
constructor(
private http: Http,
private baseUrl: string = 'http://changeYourDomainHere.com/wp-json',
private namespace: string = '/wp/v2'
private _baseUrl: string = 'http://changeYourDomainHere.com/wp-json',
private _namespace: string = '/wp/v2'
) {
this.completeUrl = `${stripTrailingSlash(this.baseUrl)}${this.namespace}`;
}

set baseUrl(val: string) { }
get baseUrl(): string { return this._baseUrl; }

set namespace(val: string) {}
get namespace(): string { return this._namespace; }

public getWebServiceUrl(postfix: string): string {
return `${this.completeUrl}${postfix}`
}
Expand Down
8 changes: 1 addition & 7 deletions src/Media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiMedia {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(mediaId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(mediaId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(mediaId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiMedia } from './interfaces';

@Injectable()
export class WpApiMedia extends WpApiParent implements IWpApiMedia {
Expand Down
12 changes: 1 addition & 11 deletions src/Pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiPages {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(pageId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(pageId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(pageId: number, options?: RequestOptionsArgs): Observable<Response>;
getMetaList(pageId: number, options?: RequestOptionsArgs): Observable<Response>;
getMeta(pageId: number, metaId: number, options?: RequestOptionsArgs): Observable<Response>;
getRevisionList(pageId: number, options?: RequestOptionsArgs): Observable<Response>;
getRevision(pageId: number, revisionId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiPages } from './interfaces';

@Injectable()
export class WpApiPages extends WpApiParent implements IWpApiPages {
Expand Down
56 changes: 41 additions & 15 deletions src/Parent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable, Inject } from '@angular/core';
import { Http } from '@angular/http';
import { Headers, Http } from '@angular/http';

// Need to import interfaces dependencies
// Bug TypeScript https://github.com/Microsoft/TypeScript/issues/5938
Expand All @@ -10,39 +10,65 @@ import { Response } from '@angular/http/src/static_response';
import { WpApiLoader } from './Loaders';
import { stripTrailingSlash } from './utils';

import { AuthSession } from './AuthSession';
import { IParent } from './interfaces';

export interface IParent {
httpGet(url: string, options?: RequestOptionsArgs): Observable<Response>;
httpHead(url: string, options?: RequestOptionsArgs): Observable<Response>;
httpDelete(url: string, options?: RequestOptionsArgs): Observable<Response>;
httpPost(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
httpPut(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
httpPatch(url: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
}

@Injectable()
export class WpApiParent implements IParent {

constructor(
public wpApiLoader: WpApiLoader,
public http: Http
) { }

protected getToken(): string {
let sessionCredentials = AuthSession.getSession();
return sessionCredentials ? sessionCredentials.token : null;
}

protected hasToken(): boolean {
return this.getToken() ? true : false;
}

protected getWebServiceUrl(postfix: string): string {
return this.wpApiLoader.getWebServiceUrl(postfix);
}

protected getDefaultOptions(
options: RequestOptionsArgs = { headers: new Headers() }
): RequestOptionsArgs {
if (!options.headers) {
options.headers = new Headers();
}
if (!options.headers.has('Authorization') && this.hasToken()) {
options.headers.append('Authorization', `Bearer ${this.getToken()}`);
}
return options;
}

httpGet(url: string, options = {}) {
return this.http.get(this.wpApiLoader.getWebServiceUrl(url), options);
options = this.getDefaultOptions(options);
return this.http.get(this.getWebServiceUrl(url), options);
}
httpHead(url: string, options = {}) {
return this.http.head(this.wpApiLoader.getWebServiceUrl(url), options);
options = this.getDefaultOptions(options);
return this.http.head(this.getWebServiceUrl(url), options);
}
httpDelete(url: string, options = {}) {
return this.http.delete(this.wpApiLoader.getWebServiceUrl(url), options);
options = this.getDefaultOptions(options);
return this.http.delete(this.getWebServiceUrl(url), options);
}
httpPost(url: string, body = {}, options = {}) {
return this.http.post(this.wpApiLoader.getWebServiceUrl(url), body, options);
options = this.getDefaultOptions(options);
return this.http.post(this.getWebServiceUrl(url), body, options);
}
httpPut(url: string, body = {}, options = {}) {
return this.http.put(this.wpApiLoader.getWebServiceUrl(url), body, options);
options = this.getDefaultOptions(options);
return this.http.put(this.getWebServiceUrl(url), body, options);
}
httpPatch(url: string, body = {}, options = {}) {
return this.http.patch(this.wpApiLoader.getWebServiceUrl(url), body, options);
options = this.getDefaultOptions(options);
return this.http.patch(this.getWebServiceUrl(url), body, options);
}
}
16 changes: 1 addition & 15 deletions src/Posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiPosts {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(postId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(postId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(postId: number, options?: RequestOptionsArgs): Observable<Response>;
getMetaList(postId: number, options?: RequestOptionsArgs): Observable<Response>;
getMeta(postId: number, metaId: number, options?: RequestOptionsArgs): Observable<Response>;
getRevisionList(postId: number, options?: RequestOptionsArgs): Observable<Response>;
getRevision(postId: number, revisionId: number, options?: RequestOptionsArgs): Observable<Response>;
getCategoryList(postId: number, options?: RequestOptionsArgs): Observable<Response>;
getCategory(postId: number, categoryId, options?: RequestOptionsArgs): Observable<Response>;
getTagList(postId: number, options?: RequestOptionsArgs): Observable<Response>;
getTag(postId: number, tagId, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiPosts } from './interfaces';

@Injectable()
export class WpApiPosts extends WpApiParent implements IWpApiPosts {
Expand Down
5 changes: 1 addition & 4 deletions src/Statuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiStatuses {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(statusesName: string, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiStatuses } from './interfaces';

@Injectable()
export class WpApiStatuses extends WpApiParent implements IWpApiStatuses {
Expand Down
5 changes: 1 addition & 4 deletions src/Taxonomies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiTaxonomies {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(taxonomiesType: string, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiTaxonomies } from './interfaces';

@Injectable()
export class WpApiTaxonomies extends WpApiParent implements IWpApiTaxonomies {
Expand Down
8 changes: 1 addition & 7 deletions src/Terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiTerms {
getList(taxonomiesType: string, options?: RequestOptionsArgs): Observable<Response>;
get(taxonomiesType: string, termId: number, options?: RequestOptionsArgs): Observable<Response>;
create(taxonomiesType: string, body: any, options?: RequestOptionsArgs): Observable<Response>;
update(taxonomiesType: string, termId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(taxonomiesType: string, termId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiTerms } from './interfaces';

const defaultTaxoType = 'categories';

Expand Down
5 changes: 1 addition & 4 deletions src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiTypes {
getList(options?: RequestOptionsArgs): Observable<Response>;
get(postType: string, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiTypes } from './interfaces';

@Injectable()
export class WpApiTypes extends WpApiParent implements IWpApiTypes {
Expand Down
9 changes: 1 addition & 8 deletions src/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,7 @@ import { WpApiParent } from './Parent';

import { WpApiLoader } from './Loaders';

export interface IWpApiUsers {
getList(options?: RequestOptionsArgs): Observable<Response>;
me(options?: RequestOptionsArgs): Observable<Response>;
get(userId: number, options?: RequestOptionsArgs): Observable<Response>;
create(body: any, options?: RequestOptionsArgs): Observable<Response>;
update(userId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(userId: number, options?: RequestOptionsArgs): Observable<Response>;
}
import { IWpApiUsers } from './interfaces';

@Injectable()
export class WpApiUsers extends WpApiParent implements IWpApiUsers {
Expand Down
17 changes: 17 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,20 @@ export interface IWpApiCustom {
update(customId: number, body: any, options?: RequestOptionsArgs): Observable<Response>;
delete(customId: number, options?: RequestOptionsArgs): Observable<Response>;
}

export interface ICredentials {
token?: string;
email?: string;
}

export interface IAuthCredentials {
username: string;
password: string;
}

export interface IWpApiAuth {
auth(authCredentials: IAuthCredentials, options?: RequestOptionsArgs): Observable<Response>;
validate(options?: RequestOptionsArgs): Observable<Response>;
saveSession(credentials: ICredentials);
removeSession();
}
Loading